我使用實體框架與代碼優先方法與導航屬性的實體框架繼承
,但我得到了一些錯誤:
用戶: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);
有誰知道這是可能的,或者我應該保持平整,不繼承會這樣?我在問,因爲該繼承將很適合我的通用存儲庫模型