2

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Autyan
  • 111
  • 1
  • 12
  • Possible duplicate of [Entity Framework Code First - two Foreign Keys from same table](http://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-table) – Kien Chu Mar 11 '16 at 04:39

2 Answers2

0

For example

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

That's all

0

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