简体   繁体   English

Dapper UpdateAsync忽略列

[英]Dapper UpdateAsync ignore column

I am trying to update with Dapper.Contrib this table: 我正在尝试使用Dapper.Contrib更新此表:

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. 我不想更新CreateUserID列,因为它是一个更新方法,所以我想在调用Dapper - Update.Async(entity)方法时忽略此列。

I tried using [NotMapped] and [UpdateIgnore] attributes but no help. 我尝试使用[NotMapped]和[UpdateIgnore]属性,但没有帮助。

Note: I still want this column to be passed on insert operations, therefore, [Computed] and [Write(false)] is not appropriate. 注意:我仍然希望在插入操作上传递此列,因此,[Computed]和[Write(false)]不合适。

Can someone help me figure out how to ignore this column when updating the table in the database? 有人可以帮我弄清楚在更新数据库中的表时如何忽略此列?

Thanks in advance. 提前致谢。

Well, it's just not supported. 好吧,它只是不受支持。 Here is related issue , and solution is expected only in Dapper v2. 这是相关问题 ,只有在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. 因此,包括未标记为Key \\ ExplicitKey \\ Computed且可写入的所有属性。 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). 对于InsertAsync也是InsertAsync (除了带有ExplicitKey属性也包含在插入中,但是你不能在你的situtaion中使用这个属性,因为你的属性毕竟不是键)。

So you have to either wait for this to be implemented, fork and implement yourself, or just write your own UpdateAsync method. 因此,您必须等待实现,自行分叉和实现,或者只编写自己的UpdateAsync方法。 You can see from source code that it's very simple and is not hard to reimplement. 您可以从源代码中看到它非常简单并且不难重新实现。

As @Evk already mentioned in his answer, there is no solution implemented yet. 正如@Evk在他的回答中已经提到的,还没有实现解决方案。 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. 除此之外,您可以选择使用Dapper( IDbConnection.Execute(...) )直接绕过此特定情况的Dapper.Contrib。

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. 我有一个类似的问题(尽管有DapperExtensions),特殊的列更新和其他复杂的查询,DapperExtensions根本无法生成或需要很多工作来实现它。

I used Dapper directly instead of DapperExtensions for that particular case; 我直接使用Dapper而不是DapperExtensions用于该特定情况; other part of project still take benefit of DapperExtensions. 项目的其他部分仍然受益于DapperExtensions。 This is like tread-off. 这就像踩踏。 Such cases are very limited. 这种情况非常有限。 I found this is better solution instead of tweaking/forcing DapperExtensions for this. 我发现这是更好的解决方案,而不是为此调整/强制DapperExtensions。 This also saved me on time and efforts. 这也节省了我的时间和努力。

I suggest using the [Computed] attribute. 我建议使用[Computed]属性。

[Computed] - this property is computed and should not be part of updates [Computed] - 此属性已计算,不应成为更新的一部分

But, It appears that the documentation for Dapper.Contrib is worded in a confusing manner. 但是,看起来Dapper.Contrib的文档措辞令人困惑。 The [Computed] attribute appears to be ignored on inserts as well - this may or may not work for your use case. [Computed]属性在插入时似乎也会被忽略 - 这可能适用于您的用例,也可能不适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM