亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

[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 關注
  • 158 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號