2016-03-11 41 views
2

我有這個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類?

我不知道的是如何將主鍵從同一個表設置爲其他字段作爲外鍵。

+0

的可能的複製[實體框架代碼優先 - 兩個外鍵從同一個表](http://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-table) –

回答

0

例如

public class WdUser : SysDomainModelBase, IWdUser 
{ 
//This is your primary key of WDUser 
public int UserID{get;set;} 
//This is for your foreign key mapping 
public virtual WDUser WDUser{get;set;} 
public virtual ICollection<WDUser> WDUserList{get;set;} 
} 

這是所有

+0

謝謝,但我有兩個WdUser屬性,我有兩個WdUserList屬性? – Autyan

+0

我沒有得到你。你問你想要2個外鍵用於同一個表(正如我解釋的那樣是一個外鍵)。如果你想要2個外鍵,你最好使用流利的API。 –

+0

是的,我只想爲同一張桌子和坦克非常多2個外鍵。 – Autyan

0

這個答案你的2個外鍵

public class WdUser : SysDomainModelBase, IWdUser 
{ 
//This is your primary key of WDUser 
public int UserID{get;set;} 
//This is for your 1st foreign key mapping 
public virtual WDUser WDUser{get;set;} 
public virtual ICollection<WDUser> WDUserList{get;set;} 
public int fk1{get;set;} 
//This is for your 2nd foreign key mapping 
public virtual ICollection<WDUser> WDUserList1{get;set;} 
public int fk2{get;set;} 
} 

的要求,你的流暢API將

//mapping for 1st foreign key 
    dbModelBuilder.Entity<WDUser>() 
         .HasKey(wdUser=> wdUser.Id) 
         .HasRequired<Tasks>(wdUser => wdUser.WDUser) 
         .WithMany(s => s.WDUserList) 
         .HasForeignKey(s => .fk1).WillCascadeOnDelete(false); 



    //mapping for 2st foreign key 
dbModelBuilder.Entity<WDUser>() 
        .HasKey(wdUser=> wdUser.Id) 
        .HasRequired<Tasks>(wdUser => wdUser.WDUser) 
        .WithMany(s => s.WDUserList1) 
        .HasForeignKey(s => .fk2).WillCascadeOnDelete(false);