question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using ID as Primary key and foreign key at same time.

See original GitHub issue

I have two entites, Prospect and person, what I’m trying to do is use Prospect.ID as primary key on ProspectTable and as foreignkey of PersonID, my ideia is use the same ID for both entities without the need of a PersonID on my Prospect entity. When the prospect is being saved on database, it tries to save a PersonID even I not have this property on my Prospect entity, I would like to know if ef core supports this kind of relationship.

Here’s what I got on my model builder.

modelBuilder.Entity<ProspectDto>(builder => { builder.ToTable("Prospects"); builder.HasKey(prospect => prospect.ID); });

modelBuilder.Entity<PersonDto>(builder => { builder.HasOne(p => p.Prospect).WithOne().HasForeignKey<ProspectDto>(pe => pe.ID); });

Here’s what is being executed on database:

INSERT INTO [Prospects] ([ID], [PersonID]) VALUES (@p421, @p422),

Thanks.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
smitpatelcommented, Feb 4, 2017

@breyed - It is ambiguous not just because of being one-to-one but also the property specified in FK attribute being present on both side. Whenever combination of ForeignKeyAttribute & InverseProperty causes ambiguous/incorrect model configuration, we throw exception because we don’t know what to make out from those attribute. It cannot be overridden by fluent API, the attribute combination needs to be corrected to be consistent.

Therefore removing annotations removes the exception. If you specifying relationship using fluent API only and facing issues then please file a new issue with relevant details

0reactions
breyedcommented, Feb 6, 2017

I’m pretty sure now that the issues with EF trying to use nonexistent columns were due to HasForeignKey and HasPrincipalKey not being specified. Thanks for the help. Based on my experience, I added #7561 as a feature request to help with troubleshooting these kinds of problems.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Primary and Foreign Key at the same time - sql server
A Foreign Key is used for referential integrity, to make sure that a value exists in another table. The Foreign key needs to...
Read more >
Can I have the same field as Primary Key and Foreign ...
Yes, it is possible to use the same field as both the primary key and foreign key in MySQL. This is called a...
Read more >
database design - Using the primary key as a foreign key
Using the foreign key as a primary key is possible. It requires you to first create the a shipment then the shipment_data reusing...
Read more >
Why Do Relational Databases Use Primary Keys and ...
Primary and foreign keys help create structure in relational databases. A primary key ensures unique row identification. This results in faster ...
Read more >
Primary and Foreign Key Constraints - SQL Server
The SalesPersonID column in the SalesOrderHeader table is the foreign key to the SalesPerson table. By creating this foreign key relationship, a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found