Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How ignore some properties in Dapper?

Tags:

c#

dapper

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
like image 407
M.Azad Avatar asked Jul 22 '15 05:07

M.Azad


1 Answers

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.

like image 72
christophano Avatar answered Nov 08 '22 18:11

christophano