one to one multiple foreign key from same table EF Core

asp.net-core entity-framework entity-framework-core

Question

Stuck on write code for table relation below :

public class PersonModel
{
    public int ID { get; set; }
    public string Name{ get; set; }
    public string Address{ get; set; }
    public DateTime? DateofBirth{ get; set; }
    public GenderEnum Gender{ get; set; }

    public PersonDetailModel PersonDetail { get; set; }
    public PersonDetailModel PersonDetailforMate { get; set; }
}

public class PersonDetailModel
{
    public int ID { get; set; }
    public int PersonID { get; set; }
    public int? MateID { get; set; }
    public string PhoneNumber{ get; set; }
    public string OfficeAddress{ get; set; }
    public MarriageStatusEnum MarriageStatus{ get; set; }

    public PersonModel Person { get; set; }
    public PersonModel PersonforMate { get; set; }
}

I use Fluent API :

modelBuilder.Entity<PersonModel>()
            .HasOne(d => d.PersonDetailModel)
            .WithOne(p => p.PersonModel)
            .HasForeignKey<PersonDetailModel>(d => d.PersonID);

modelBuilder.Entity<PersonDetailModel>()
            .HasOne(d => d.PersonModel)
            .WithOne(p => p.PersonDetailModel)
            .HasForeignKey<PersonDetailModel>(d => d.MateID);

when i build, it getting error

Cannot create a relationship between 'PersonModel.PersonDetailforMate' and 'PersonDetailModel.Person', because there already is a relationship between 'PersonModel.PersonDetail' and 'PersonDetailModel.Person'. Navigation properties can only participate in a single relationship.

there are several references for similar problem :

but those are for one-to-many relationship.

*sorry for my bad english

1
0
4/2/2020 10:50:19 AM

Popular Answer

I had a similar situation in my project, and this is what I ended up doing:

public class PersonModel
{
    public int ID { get; set; }
    public string Name{ get; set; }
    public string Address{ get; set; }
    public DateTime? DateofBirth{ get; set; }
    public GenderEnum Gender{ get; set; }

    public int PersonID { get; set; }
    [ForeignKey("PersonID ")]
    public PersonDetailModel PersonDetail { get; set; }

    public int? MateID { get; set; }
    [ForeignKey("MateID")]
    public PersonDetailModel PersonDetailforMate { get; set; }
}

public class PersonDetailModel
{
    public int ID { get; set; }
    public string PhoneNumber{ get; set; }
    public string OfficeAddress{ get; set; }
    public MarriageStatusEnum MarriageStatus{ get; set; }
}

so PersonModel would have two foreign keys to PersonDetailModel. I am using ForeignKey attribute to explicitly tell EF which foreign key belongs to which relationship - if you use the Foreignkey attribute, you won't need the Fluent API Configurations...

The above code will build you a PersonModel table in the DB, with PersonId and MateId columns as foreign key.

1
11/13/2018 7:27:27 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