我有這個WdUser
類從SysModelBase
繼承外鍵:實體框架代碼優先設置在同一個表的主鍵
[Serializable]
public class WdUser : SysDomainModelBase, IWdUser
{
[Display(Name = "UserName")]
[MaxLength(25)]
[Required]
public string UserName { get; set; }
[Required]
[Index(IsUnique = true)]
[MaxLength(25)]
[Display(Name = "LoginName ")]
public string LoginName { get; set; }
[Display(Name = "Password ")]
[MaxLength(50)]
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Email 地址")]
[DataType(DataType.EmailAddress)]
[MaxLength(50)]
public string Email { get; set; }
[Display(Name = "Telephone ")]
[DataType(DataType.PhoneNumber)]
[MaxLength(15)]
public string Telephone { get; set; }
[Display(Name = "LastLoginDateTime ")]
[DataType(DataType.DateTime)]
public DateTime LastLoginDateTime { get; set; }
[Required]
[Display(Name = "UserStatus ")]
public UserStatus Status { get; set; }
[Display(Name = "Roles ")]
public List<WdRole> Roles { get; set; }
public bool IsInRole(string role) => Roles.Any(item => item.RoleName == role);
[NotMapped]
public IIdentity Identity { get; set; }
}
其中SysModelBase
就像
[Serializable]
public class SysModelBase : ModelBase, ISysModel
{
[JsonIgnore]
[DataType(DataType.DateTime)]
public DateTime CreateDateTime { get; set; }
[JsonIgnore]
public WdUser CreateUser { get; set; }
[JsonIgnore]
[DataType(DataType.DateTime)]
public DateTime LastUpdateDateTime { get; set; }
[JsonIgnore]
public WdUser LastUpdateUser { get; set; }
[JsonIgnore]
public bool IsDeleted { get; set; }
public bool IsEnabled { get; set; }
}
當我運行這段代碼,我收到一個錯誤:
Unable to determine the principal end of an association between the types 'SHWDTech.Platform.Model.Model.WdUser' and 'SHWDTech.Platform.Model.Model.WdUser'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
應該如何設置關聯nship與WdUser
類?
我不知道的是如何將主鍵從同一個表設置爲其他字段作爲外鍵。
的可能的複製[實體框架代碼優先 - 兩個外鍵從同一個表](http://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-table) –