[C# - dapper] ExplicitKey

오랫만에 dapper 사이트에 들어갔더니 묻고 답해요 코너가 생겼다.

 

그래서 ExplicitKey에 대해 물어보았더니 드디어 정확한 정의를 찾았다. 

 

The ExplicitKey attribute is used in Dapper to explicitly specify that a property should be treated as the primary key for a table. By default, Dapper assumes that the property with the name "Id" or "ID" is the primary key. However, if your table has a different primary key property name, you can use the ExplicitKey attribute to indicate which property should be used as the primary key.

 

-->


ExplicitKey 특성은 Dapper에서 속성이 테이블의 기본 키로 처리되어야 함을 명시적으로 지정하는 데 사용됩니다. 기본적으로 Dapper는 이름이 "Id" 또는 "ID"인 속성이 기본 키라고 가정합니다. 그러나 테이블에 다른 기본 키 속성 이름이 있는 경우 ExplicitKey 특성을 사용하여 기본 키로 사용해야 하는 속성을 나타낼 수 있습니다.

 

 

그래서 만약 내가 설정한 테이블에 GetAll<>()  사용시 ExplicitKey 를 설정하지 않으면 에러가 난다. 

 

+) 방식의 select  쿼리를 사용하면 test_table에 내부적으로 설정된 index가 있으면 해당 index를 사용하여 수행한다. 

var sql = "SELECT * FROM test_table WHERE idx = @idx";
var result = connection.Query<Test_table>(sql, new {idx = idx}).ToList();