How to ignore some properties in Dapper?

I have a simple class:

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

The column in the Db tabale column is set to auto. When I insert user with dapper

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

Dapper generates a parameter for the all property, and I get an error similar to this

The procedure or function User_Insert contains too many arguments.

How to ignore rowversion in dapper parameter?

My procedure code:

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

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

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

Alternatively, you can use the Dapper Contrib extensions and create a mapping class that allows you to ignore certain properties.

+2
source

All Articles