为了账号安全,请及时绑定邮箱和手机立即绑定

[Compated] 和 [Write(false)] 属性之间有什么区别?

[Compated] 和 [Write(false)] 属性之间有什么区别?

C#
慕容森 2023-09-24 11:05:38
此资源解释了如何Computed排除属性(仅在更新中?)。指定应从更新中排除的属性。[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)]?编辑:我刚刚检查了针对我的问题链接的资源。几乎就说到这里了!有人可以确认这两个属性是否执行相同的操作,但只是以两种不同的方式措辞,以便为用户提供更好的抽象?
查看完整描述

1 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

[Computed]都会Write(false)忽略属性 whileINSERT以及UPDATE操作。所以,它们两者是相同的。您可以使用其中任何一种。

文档如下:

  • [Write(true/false)]- 该属性不可写

  • [Computed]- 该属性是计算得出的,不应成为更新的一部分

关于Write

如上面文档的第一行所述,Write处理“可写”行为。这应该包括INSERTUPDATE


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

上面文档中的第二行有点宽泛。

不应成为更新的一部分

这是否意味着它可以成为 的一部分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);

}

通过上面的代码,您将观察到无论您选择哪个属性来装饰属性,它都不会被考虑 forINSERT或 for UPDATE。所以基本上,这两个属性都扮演着相同的角色。

这可以在 github 上的Dapper.Tests.Contrib测试项目中得到进一步证实。

[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" });


查看完整回答
反对 回复 2023-09-24
  • 1 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信