gpt4 book ai didi

c# - Dapper UpdateAsync 忽略列

转载 作者:可可西里 更新时间:2023-11-01 08:11:52 26 4
gpt4 key购买 nike

我正在尝试使用 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; }
}

我不想更新 CreateUserID 列,因为它是一种更新方法,因此我想在调用 Dapper - Update.Async(entity) 方法时忽略此列。

我尝试使用 [NotMapped] 和 [UpdateIgnore] 属性但没有帮助。

注意:我仍然希望在插入操作时传递此列,因此,[Computed] 和 [Write(false)] 是不合适的。

谁能帮我弄清楚如何在更新数据库中的表时忽略此列?

最佳答案

嗯,它只是不受支持。这里有相关issue ,并且仅在 Dapper v2 中才有解决方案。您还可以检查 source code (这很简单)并看到更新的属性被搜索如下:

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

因此,所有未标记为 Key\ExplicitKey\Computed 且可写的属性都包含在内。 InsertAsync 也是如此(除了带有 ExplicitKey 的属性也包含在 insert 中,但是你不能在你的情况下使用这个属性,因为你的属性毕竟不是键)。

因此,您必须等待它被实现、 fork 并自己实现,或者只编写您自己的 UpdateAsync 方法。从源码可以看出非常简单,重新实现起来也不难。

关于c# - Dapper UpdateAsync 忽略列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47639596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com