2016-04-12 53 views
2

我使用實體框架與代碼優先方法與導航屬性的實體框架繼承

enter image description here

,但我得到了一些錯誤:

用戶:FromRole:NavigationProperty「用戶'無效。在AssociationType'User_SoteAccounts'中輸入FromRole的'SoteAccount'User_SoteAccounts_Target'必須與聲明此NavigationProperty的'AllegroAccount'類型完全匹配。
AllegroAccount_Template_Source::多重性在'AllegroAccount_Template'關係中的角色'AllegroAccount_Template_Source'中無效。由於依賴角色屬性不是關鍵屬性,所以從屬角色的多重性的上限必須是''。
SoteAccount_Template_Source::多重性在'SoteAccount_Template'關係中的角色'SoteAccount_Template_Source'中無效。因爲「依賴角色」屬性不是關鍵屬性,所以「從屬角色」的多重性上限必須爲「
」。

它甚至有可能繼承引用類嗎?

這裏是類和onModelCreating

[Table("AllegroAccounts")] 
public class AllegroAccount : ShopAccountBase 
{ 
    public string AllegroLogin { get; set; } 
    public string AllegroPassword { get; set; } 
    public string AllegoWebApiKey { get; set; } 
    public int CountryCode { get; set; }  
} 

public class ShopAccountBase : AccountBase 
{ 
    public int TemplateForeignKey { get; set; } 
    [ForeignKey("TemplateForeignKey")] 
    public Template Template { get; set; } 
} 

public abstract class AccountBase 
{ 
    [Key] 
    public int AccountBaseId { get; set; } 
    public bool IsActive { get; set; } 
    public int UserForeignKey { get; set; } 
    [ForeignKey("UserForeignKey")] 
    public virtual User User { get; set; } 
    public bool DaysCountActive { get; set; } 
    public int DaysCount { get; set; } 
} 

public class Template 
{ 
    public Template() 
    { 
     AdditionalServices = new AdditionalServices(); 
     BasicServices = new BasicServices(); 
     TemplatePackages = new ObservableCollection<TemplatePackage>(); 
    } 

    [Key] 
    public int TemplateID { get; set; } 

    public string TemplateName { get; set; } 
    public TemplateKind? TemplateKind { get; set; } 
    public CourierFirm? CourierFirm { get; set; } 
    public int Used { get; set; } 
    public virtual ICollection<TemplatePackage> TemplatePackages { get; set; } 
    public string ExternalNumber { get; set; } 
    public string MPKNumber { get; set; } 
    public AdditionalServices AdditionalServices { get; set; } 
    public BasicServices BasicServices { get; set; } 
    public string Description { get; set; } 
    [Column(TypeName = "datetime")] 
    public DateTime? CreationDate { get; set; } 
} 

public class User 
{ 
    public User() 
    { 
     DefaultReturnAddress = new Address(); 
     DefaultSendingAddress = new Address(); 
     PersonInfoSending = new PersonInfo(); 
     PersonInfoReturning = new PersonInfo(); 
     AdditionalServices = new AdditionalServices(); 

     WayBillLabel = new WaybillLabel(); 
     Settings = new UserSettings(); 
     AllegroAccounts = new ObservableCollection<AllegroAccount>(); 
     InpostAccounts = new ObservableCollection<InpostAccount>(); 
     TbaAccounts = new ObservableCollection<TbaAccount>(); 
     TruckerAccounts = new ObservableCollection<TruckerAccount>(); 
    } 

    [Key] 
    public int UserId { get; set; } 

    public byte[] Password { get; set; } 
    public string Login { get; set; } 

    public Address DefaultReturnAddress { get; set; } 
    public Address DefaultSendingAddress { get; set; } 

    public PersonInfo PersonInfoSending { get; set; } 
    public PersonInfo PersonInfoReturning { get; set; } 

    public string MPKnumReturn { get; set; } 
    public string MPKnumSending { get; set; } 

    public AdditionalServices AdditionalServices { get; set; } 

    public float MaxLength { get; set; } 
    public float MaxWidth { get; set; } 
    public float MaxHeight { get; set; } 
    public float MaxWeight { get; set; } 

    public int FileTemplateId { get; set; } 
    public string CollectiveShipmentFilePath { get; set; } 

    private PermissionFlags _permissions; 

    public PermissionFlags Permissions 
    { 
     get { return _permissions; } 
     set 
     { 
      if (_permissions.HasFlag(value)) { _permissions &= ~value; } 
      else { 
       _permissions |= value; 
      } 
     } 
    } 

    public TemplatingMethod TemplatingMethod { get; set; } 

    public UserSettings Settings { get; set; } 

    public WaybillLabel WayBillLabel { get; } 

    public ICollection<AllegroAccount> AllegroAccounts { get; set; } 
    public ICollection<SoteAccount> SoteAccounts { get; set; } 

    public ICollection<InpostAccount> InpostAccounts { get; set; } 
    public ICollection<TruckerAccount> TruckerAccounts { get; set; } 
    public ICollection<TbaAccount> TbaAccounts { get; set; } 

    // this is the right property to use for modifying the collection 
    public ICollection<string> AvailableMpksCollection { get; set; } 

    // this is computed property for Entity Framework only, because it cannot store a collection of primitive type 
    public string AvailableMpksString 
    { 
     get { return AvailableMpksCollection != null ? string.Join(",", AvailableMpksCollection) : null; } 
     set { 
      AvailableMpksCollection = !string.IsNullOrEmpty(value) ? value.Split(',').ToList() : new List<string>(); 
     } 
    } 
} 


modelBuilder.Entity<AllegroAccount>().HasOptional(account => account.Template).WithOptionalDependent(); 

modelBuilder.Entity<User>() 
      .HasMany<AllegroAccount>(u => u.AllegroAccounts) 
      .WithOptional(acc => acc.User) 
      .HasForeignKey(acc => acc.UserForeignKey); 

modelBuilder.Entity<SoteAccount>().HasOptional(account => account.Template).WithOptionalDependent(); 

modelBuilder.Entity<User>() 
      .HasMany<SoteAccount>(u => u.SoteAccounts) 
      .WithOptional(acc => acc.User) 
      .HasForeignKey(acc => acc.UserForeignKey); 

有誰知道這是可能的,或者我應該保持平整,不繼承會這樣?我在問,因爲該繼承將很適合我的通用存儲庫模型

回答

1

這很可能是因爲您正在定義[ForeignKey]屬性並在流利配置中配置外鍵。

您已經在流利配置中定義了(AllegroAccount和User)和(SoteAccount和User)之間的鏈接,當您的AccountBase已經使用[ForeignKey]定義了這個鏈接時。

對於您的模板鏈接,最有可能是同樣的事情 - 該關係在ShopAccountBase級別由[ForeignKey]屬性定義 - 您不需要爲流暢配置中的繼承類重新定義它。

嘗試刪除所有你的modelBuilder流利的配置條目 - 它應該仍然通過繼承關係工作。