簡體   English   中英

如何使用自定義 class 作為具有 Entity Framework Core 轉換的實體的屬性?

[英]How do you use a custom class as a property of an entity with a conversion with Entity Framework Core?

我正在嘗試在我的應用程序中使用“每個層次結構的表”來 model 各種級別的帳戶。 基本上有3個級別的帳戶:超級,合作伙伴和標准。 它們都共享大部分屬性,但主要區別在於標准帳戶由超級帳戶或合作伙伴帳戶管理。

我想將AccountType屬性添加到Account庫 class 中,該屬性是自定義 class,但作為 string 保存到數據庫中。 我知道您可以通過 EF 的 fluent API 添加屬性轉換,但我無法讓它在這種情況下工作。

這就是我的課程的布局方式:

 public abstract class Account { [Key] public int Id { get; set; } [Required] public AccountTypeClass AccountType { get; set; } // other props omitted for brevity } public class StandardAccount: Account { [Required] public int ManagingAccountId { get; set; } [ForeignKey(nameof(ManagingAccountId))] public ManagingAccount ManagingAccount { get; set; } } public abstract class ManagingAccount: Account { public ICollection<StandardAccount> Accounts { get; set; } = new List<StandardAccount>(); } public class PartnerAccount: ManagingAccount { } public class SuperAccount: ManagingAccount { } public class AccountTypeClass { // other props omitted for brevity private string value; private AccountTypeClass(string value) => this.value = value; public static AccountTypeClass Super => new AccountTypeClass(nameof(Super).ToLower()); public static AccountTypeClass Partner => new AccountTypeClass(nameof(Partner).ToLower()); public static AccountTypeClass Standard => new AccountTypeClass(nameof(Standard).ToLower()); public static AccountTypeClass Parse(string value) { switch(value) { case "super": return Super; case "partner": return Partner; case "standard": return Standard; default: throw new NotImplementedException(); } } public override string ToString() => this.value; }

這是我的DbContext

 public class DataContext: DbContext { public DataContext(DbContextOptions options): base(options) { } public virtual DbSet<Account> Accounts { get; set; } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity<Account>().Property(p => p.AccountType).HasConversion(p => p.ToString(), p => AccountTypeClass.Parse(p)); builder.Entity<SuperAccount>().HasData(new SuperAccount() { Id = 1 }); builder.Entity<PartnerAccount>().HasData(new PartnerAccount() { Id = 2 }); builder.Entity<StandardAccount>().HasData(new StandardAccount() { Id = 3, ManagingAccountId = 1 }); } }

當我嘗試添加遷移時,我收到以下錯誤消息:

無法將屬性或導航“AccountType”添加到實體類型“ManagingAccount”,因為實體類型“Account”上已存在同名的屬性或導航。

但是,如果我為此枚舉換出AccountTypeClass

 public enum AccountTypeEnum { Super, Partner, Standard }

並將轉換更改為此(其中EnumHelper只是一個小助手 class ,它將 string 解析為枚舉值):

 builder.Entity<Account>().Property(p => p.AccountType).HasConversion(p => p.ToString(), p => EnumHelper.Parse<AccountTypeEnum>(p));

一切都按預期工作。

我可以在獨立的 class(無繼承)上進行與AccountTypeClass的轉換,但當我嘗試使用 inheritance 和每個層次結構的表時,則不能。 這是不支持還是我做錯了什么?

我可能是錯的,但我覺得你的遷移現在搞砸了。 您是否嘗試過刪除所有遷移並創建新的初始遷移,只是為了驗證您的 model 是可翻譯的?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM