one to one multiple foreign key from same table EF Core












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 :




  • Multiple foreign keys pointing to same table in Entity Framework 4.1 code first

  • 2 Foreign Keys as Primary Key using EF Core 2.0 Code First

  • EF multiple foreign key relationship on same primary key


but those are for one-to-many relationship.



*sorry for my bad english










share|improve this question



























    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 :




    • Multiple foreign keys pointing to same table in Entity Framework 4.1 code first

    • 2 Foreign Keys as Primary Key using EF Core 2.0 Code First

    • EF multiple foreign key relationship on same primary key


    but those are for one-to-many relationship.



    *sorry for my bad english










    share|improve this question

























      0












      0








      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 :




      • Multiple foreign keys pointing to same table in Entity Framework 4.1 code first

      • 2 Foreign Keys as Primary Key using EF Core 2.0 Code First

      • EF multiple foreign key relationship on same primary key


      but those are for one-to-many relationship.



      *sorry for my bad english










      share|improve this 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 :




      • Multiple foreign keys pointing to same table in Entity Framework 4.1 code first

      • 2 Foreign Keys as Primary Key using EF Core 2.0 Code First

      • EF multiple foreign key relationship on same primary key


      but those are for one-to-many relationship.



      *sorry for my bad english







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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 6:41









      Pandu WijayaPandu Wijaya

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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.






          share|improve this answer


























          • 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 9:59











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275214%2fone-to-one-multiple-foreign-key-from-same-table-ef-core%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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.






          share|improve this answer


























          • 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 9:59
















          0














          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.






          share|improve this answer


























          • 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 9:59














          0












          0








          0







          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.






          share|improve this 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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 7:27

























          answered Nov 13 '18 at 7:19









          Hooman BahreiniHooman Bahreini

          3,2633730




          3,2633730













          • 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 9:59



















          • 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 9:59

















          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 9:59





          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 9:59


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275214%2fone-to-one-multiple-foreign-key-from-same-table-ef-core%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma