0

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

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
Pandu Wijaya
  • 1
  • 1
  • 1

1 Answers1

1

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.

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
  • i think it's a cool idea. but, it will change my master table (person). and i cant get mate's name, address and other detail – Pandu Wijaya Nov 14 '18 at 09:59