How ignore some properties in Dapper?

c# dapper

Question

I have a simple class like this :

public class User
{
    public Guid Id{ get; set; }
    public string UserName { get; set; }
    public byte[] RowVersion { get; set; }
}

Rowversion column in Db tabale get auto value.when I Inser an user with dapper

var result = db.Execute("[dbo].[User_Insert]", user, trans,commandType:CommandType.StoredProcedure);

Dapper generate parameter for all property and I get error like this

Procedure or function User_Insert has too many arguments specified.

How can I ignore Rowversion in Dapper parameter?

My procedure code is:

 ALTER PROCEDURE [In4].[User_Insert] 
       @Id         uniqueidentifier,
       @UserName    nvarchar(4000)= NULL   
AS 
BEGIN
   insert into [In4].[Users] ([Id], [UserName])
                 VALUES (@Id, @UserName) 
End

Popular Answer

You could pass in an anonymous object with only the properties you want.

var result = db.Execute("[dbo].[User_Insert]", new { user.Id, user.UserName }, trans, commandType:CommandType.StoredProcedure);

Alternatively, you could use the Dapper Contrib extensions and create a mapping class, which allows you to ignore specific properties.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why