Entity Framework code-first set foreign key with primary key in same table

asp.net c# entity-framework-6

Question

I have this WdUser class inherited from 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; }
}

which the SysModelBase is like

[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; }
}

When I run this code, I get an error:

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.

How should I set the relationship with WdUser class?

What I don't know is how to set the primary key from same table to other fields as foreign key.

1
2
3/11/2016 5:42:43 AM

Popular Answer

This answer for your requirement of 2 foreign keys

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

your fluent api will be

//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); 
0
3/13/2016 7:44:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow