实体框架代码优先在同一个表中设置具有主键的外键

时间:2016-03-11 04:24:10

标签: c# asp.net entity-framework-6

我从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; }
}

当我运行此代码时,出现错误:

  

无法确定类型之间关联的主要结尾&#;; SHWDTech.Platform.Model.Model.WdUser&#39;和&#39; SHWDTech.Platform.Model.Model.WdUser&#39;。必须使用关系流畅API或数据注释显式配置此关联的主要结尾。

我应该如何设置与WdUser类的关系?

我不知道如何将主键从同一个表设置为其他字段作为外键。

2 个答案:

答案 0 :(得分: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;}
}

这就是全部

答案 1 :(得分: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);