Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 226
Misha Zaslavsky Avatar asked Dec 04 '17 18:12

Misha Zaslavsky


Video Answer


3 Answers

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.

like image 135
Evk Avatar answered Oct 21 '22 02:10

Evk


As @Evk already mentioned in his answer, there is no solution implemented yet. He have also mentioned the workarounds.

Apart from that, you can choose to use Dapper (IDbConnection.Execute(...)) directly bypassing Dapper.Contrib for this particular case.

I had a similar problem (with DapperExtensions though) with particular column update and other complex queries those DapperExtensions either cannot generate at all or need much work to make it happen with it.

I used Dapper directly instead of DapperExtensions for that particular case; other part of project still take benefit of DapperExtensions. This is like tread-off. Such cases are very limited. I found this is better solution instead of tweaking/forcing DapperExtensions for this. This also saved me on time and efforts.

like image 34
Amit Joshi Avatar answered Oct 21 '22 02:10

Amit Joshi


I suggest using the [Computed] attribute.

[Computed] - this property is computed and should not be part of updates

But, It appears that the documentation for Dapper.Contrib is worded in a confusing manner. The [Computed] attribute appears to be ignored on inserts as well - this may or may not work for your use case.

like image 1
BlakeH Avatar answered Oct 21 '22 02:10

BlakeH