13

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?

4

3 回答 3

3

好吧,它只是不支持。这是相关问题,仅在 Dapper v2 中才有解决方案。您还可以检查源代码(非常简单)并查看更新的属性搜索如下:

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

因此,所有未标记为 Key\ExplicitKey\Computed 且可写的属性都包括在内。同样的情况发生InsertAsync(除了属性ExplicitKey也包含在插入中,但您不能在您的情况下使用此属性,因为您的属性毕竟不是关键)。

所以你要么等待它被实现,然后自己分叉和实现,或者只是编写你自己的UpdateAsync方法。从源代码可以看出,它非常简单,不难重新实现。

于 2017-12-07T21:18:06.323 回答
2

正如@Evk 在他的回答中已经提到的那样,还没有实施解决方案。他还提到了解决方法。

除此之外,对于这种特殊情况,您可以选择IDbConnection.Execute(...)直接绕过 Dapper.Contrib 使用 Dapper ()。

我有一个类似的问题(尽管有 DapperExtensions),特定的列更新和其他复杂的查询,这些 DapperExtensions 要么根本无法生成,要么需要做很多工作才能让它发生。

对于那种特殊情况,我直接使用了 Dapper 而不是 DapperExtensions;项目的其他部分仍然受益于 DapperExtensions。这就像踩踏。这种情况非常有限。我发现这是更好的解决方案,而不是为此调整/强制使用 DapperExtensions。这也节省了我的时间和精力。

于 2017-12-09T06:58:58.627 回答
1

我建议使用该[Computed]属性。

[Computed]- 这个属性是计算出来的,不应该是更新的一部分

但是,似乎 Dapper.Contrib 的文档措辞令人困惑。该[Computed]属性似乎在插入时也被忽略 - 这可能适用于您的用例,也可能不适用。

于 2017-12-07T13:50:43.287 回答