Reputation: 367
In Dapper how do you ignore a table column during an insert?
I have a class with properties A,B,C and have a List of these.
Presume something like
class DTO
{
string A;
string B;
string C;
}
and the list is of type
List myList = new List<DTO>()
So with a line of sql like this to bulk insert the list into my table
sql="INSERT INTO TABLE VALUES (@A, @B, @C)";
conn.Execute(sql, myList)
It works fine and inserts all my data when my table also has columns A,B,C.
But when my table has more columns, e.g. A,B,C,D I get the error: Column name or number of supplied values does not match table definition
I know how to ignore class properties using the Dapper Contrib library, but not the other way round.
How can I do this?
Thanks.
Upvotes: 2
Views: 1371
Reputation: 180777
You have to enumerate your columns specifically, as in:
sql="INSERT INTO TABLE (column1name, column2name, column3name) VALUES (@A, @B, @C)";
Alternatively, if your DTO contains a primary key, you can decorate your DTO with the following attributes:
[Table("TableName")]
class DTO
{
[Key]
int id;
string A;
string B;
string C;
}
and use Insert()
from Dapper.Contrib.
connection.Insert(dto);
Upvotes: 2