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

asp.net c# entity-framework-6

Popular Answer

This response relates to your request for two 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 API will be fluent.

//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