Search code examples
c#dapperdapper-contrib

Dapper UpdateAsync ignore column


I am trying to update with Dapper.Contrib this table:

public class MyTable
{
    public int ID { get; set; }
    public int SomeColumn1 { get; set; }
    public int SomeColumn2 { get; set; }
    public int CreateUserID { get; set; }
    public int UpdateUserID { get; set; }
}

I don't want to update the CreateUserID column because it is an update method so that I want to ignore this column while calling the Dapper - Update.Async(entity) method.

I tried using [NotMapped] and [UpdateIgnore] attributes but no help.

Note: I still want this column to be passed on insert operations, therefore, [Computed] and [Write(false)] is not appropriate.

Can someone help me figure out how to ignore this column when updating the table in the database?


Solution

  • Well, it's just not supported. Here is related issue, and solution is expected only in Dapper v2. You can also inspect source code (it's pretty simple) and see that updated properties are searched as follows:

     var allProperties = TypePropertiesCache(type);
     keyProperties.AddRange(explicitKeyProperties);
     var computedProperties = ComputedPropertiesCache(type);
     var nonIdProps = allProperties.Except(keyProperties.Union(computedProperties)).ToList();
    

    So all properties not marked with Key\ExplicitKey\Computed and which are writable are included. The same happens for InsertAsync (except properties with ExplicitKey are also included in insert, but you cannot use this attribute in your situtaion, because your property is not key after all).

    So you have to either wait for this to be implemented, fork and implement yourself, or just write your own UpdateAsync method. You can see from source code that it's very simple and is not hard to reimplement.