看到這個頁面后,說:
支持欄位允許 EF 讀取和/或寫入欄位而不是屬性。... 默認情況下,EF 將始終讀取和寫入支持欄位 - 假設已正確配置一個 - 并且永遠不會使用該屬性。
我想我會試一試。該屬性是 EF Core 不支持的字串陣列,所以我想我可以使用字串作為支持欄位。
private string ImagesString;
[BackingField(nameof(ImagesString))]
public string[] Images
{
get
{
return ImagesString.Split('\n');
}
set
{
ImagesString = string.Join('\n', value);
}
}
我的假設是 EF Core 不會嘗試保存,Images而只會嘗試保存ImagesString哪個是純字串。但是,當我嘗試它時,我得到了相同的“Images無法映射該屬性,因為它屬于string[]...型別”為什么要嘗試保存Images?它不應該只嘗試保存/恢復ImagesString嗎?
uj5u.com熱心網友回復:
首先,你忘了放進OnModelCreating的方法的DbContext UsePropertyAccessMode(它也是在你提供的鏈接中提到)。
但是,您會得到:
The specified field 'ImagesString' of type 'string' cannot be used for the property 'Class.Images' of type 'string[]'. Only backing fields of types that are compatible with the property type can be used.因此,我認為 EF 現在無法處理此轉換。
我建議您根據需要在物體中創建公共方法或 [NotMapped] 屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350459.html
下一篇:EntityFramework5.0Contains/Equals/StartsWith/EndWithinAny不能像在EFCore2.2中那樣在SQL中進行轉換
