我正在使用实体框架和代码优先方法
但是我收到了一些错误:
用户:FromRole:NavigationProperty“用户”无效。在AssociationType'User_SoteAccounts'中输入FromRole'User_SoteAccounts_Target'的'SoteAccount'必须与声明此NavigationProperty的类型'AllegroAccount'完全匹配。
AllegroAccount_Template_Source :: Multiplicity在关系'AllegroAccount_Template'中的角色'AllegroAccount_Template_Source'中无效。由于“从属角色”属性不是关键属性,因此从属角色的多重性的上限必须为“”。
SoteAccount_Template_Source ::多重性在关系'SoteAccount_Template'中的角色'SoteAccount_Template_Source'中无效。由于Dependent Role属性不是关键属性,因此Dependent Role的多重性的上限必须为“”。
甚至可以继承带引用的类吗?
以下是类和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);
有没有人知道这是否可能,或者我应该保持不变并且不继续这样做?我问,因为继承会很好地适应我的通用存储库模型
答案 0 :(得分:3)
这可能是因为您正在定义[ForeignKey]
属性并在流畅配置中配置外键。
当您的AccountBase已使用[ForeignKey]定义时,您已在流畅配置中定义了(AllegroAccount和用户)和(SoteAccount和用户)之间的链接。
同样的事情很可能适用于您的模板链接 - 关系是ShopAccountBase
级别由[ForeignKey]
级别定义的 - 您不需要为继承的类重新定义它在流畅的配置。
尝试删除所有modelBuilder
流畅的配置条目 - 它仍然可以继承关系。