This资源解释如何Computed
排除属性(仅在更新中?)。
Specifie the property should be excluded from update.
[Table("Invoice")] public class InvoiceContrib { [Key] public int InvoiceID { get; set; } public string Code { get; set; } public InvoiceKind Kind { get; set; } [Write(false)] [Computed] public string FakeProperty { get; set; } } using (var connection = My.ConnectionFactory()) { connection.Open(); var invoices = connection.GetAll<InvoiceContrib>().ToList(); // The FakeProperty is skipped invoices.ForEach(x => x.FakeProperty += "z"); var isSuccess = connection.Update(invoices); }
没有
Write(false)
达到同样的目的? [Computed]
有什么区别和 [Write(false)]
?编辑:
我刚刚查看了资源 linked回答我的问题。它几乎一针见血!有人可以确认这两个属性是否执行相同的操作,但只是以两种不同的方式表达,以便为用户提供更好的抽象?
最佳答案
两者 [Computed]
和 Write(false)
将忽略该属性,而 INSERT
以及 UPDATE
操作。所以,两者都是一样的。您可以使用其中任何一种。
Documentation下面说:
[Write(true/false)]
- this property is (not) writeable[Computed]
- this property is computed and should not be part of updates
关于
Write
:如上述文档第一行所述,
Write
处理“可写”行为。这应该包括 INSERT
和 UPDATE
.这也可以在源代码here中得到确认:
var properties = type.GetProperties().Where(IsWriteable).ToArray(); ... ... ... private static bool IsWriteable(PropertyInfo pi) { var attributes = pi.GetCustomAttributes(typeof(WriteAttribute), false).AsList(); if (attributes.Count != 1) return true; var writeAttribute = (WriteAttribute)attributes[0]; return writeAttribute.Write; }
关于
Computed
:不过,上面文档中的第二行有点宽泛。
should not be part of updates
这是否意味着它可以是
INSERT
的一部分? ?不,不是的;它还涵盖了这两个操作。这可以通过以下代码观察:CREATE TABLE TestTable
(
[ID] [INT] IDENTITY (1,1) NOT NULL CONSTRAINT TestTable_P_KEY PRIMARY KEY,
[Name] [VARCHAR] (100) NOT NULL,
[ComputedCol] [VARCHAR] (100) NOT NULL DEFAULT '',
[NonWriteCol] [VARCHAR] (100) NOT NULL DEFAULT ''
)
[Table("TestTable")]
public class MyTable
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
[Computed]
public string ComputedCol { get; set; }
[Write(false)]
public string NonWriteCol { get; set; }
}
int id;
using(SqlConnection conn = new SqlConnection(@"connection string"))
{
MyTable myTable = new MyTable();
myTable.Name = "Name";
myTable.ComputedCol = "computed";
myTable.NonWriteCol = "writable";
conn.Insert<MyTable>(myTable);
id = myTable.ID;
}
using(SqlConnection conn = new SqlConnection(@"connection string"))
{
MyTable myTable = conn.Get<MyTable>(id);
myTable.Name = "Name_1";
myTable.ComputedCol = "computed_1";
myTable.NonWriteCol = "writable_1";
conn.Update<MyTable>(myTable);
}
通过上面的代码,你会发现无论你选择哪个属性来装饰属性,
INSERT
都不会考虑它。也不适用于 UPDATE
.所以基本上,这两个属性都扮演着同样的角色。这可以在 Dapper.Tests.Contrib 中进一步确认github上的测试项目。
[Table("Automobiles")] public class Car { public int Id { get; set; } public string Name { get; set; } [Computed] public string Computed { get; set; } } ... ... ... //insert with computed attribute that should be ignored connection.Insert(new Car { Name = "Volvo", Computed = "this property should be ignored" });
查看注释和上面代码中分配给属性的值,很明显
Computed
还应该忽略 INSERT
的属性手术;这是测试的预期结果。为什么为同一目的提供这两种方式尚不清楚。它会引起困惑。
以下是一些额外的引用:
Comment 1
I use
[Computed]
or[Write("False")]
for that. Does that not work for your scenario?
Comment 2
Glad I could help. Every day is a school day! I'm not sure why they both exist though as I think they are functionally the same. I tend to use
[Computed]
just because it is marginally easier to type.
Comment 3
I understand that using Dapper.Contrib I can use the
Write
andComputed
attributes to ignore properties during write operations. However, this will ignore the properties on both insert and update. I need a way to ignore properties on updates. My suggestion would be to add 2 attributes... perhaps namedInsertable(bool)
andUpdateable(bool)
. When afalse
value is passed to these the framework would exclude that property for the given operation. This is a lightweight, straightforward approach to a very common problem.
我不认为
Computed
属性与Computed Columns有关因为 Dapper.Contrib 支持多个 RDBMS。
关于c# - [Computed] 和 [Write(false)] 属性之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57673107/