one to one multiple foreign key from same table EF Core
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
add a comment |
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
add a comment |
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
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
entity-framework asp.net-core entity-framework-core
asked Nov 13 '18 at 6:41
Pandu WijayaPandu Wijaya
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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