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.

Is the table structure for 1:1 relations backwards?

See original GitHub issue

Will try to make this as concise as possible:

image

Given the example above, the relation to UserInformation is defined in my User class. My UserInformation rows are not really useful unless they specify a UserId, but I’ll always be navigating from the other direction. That is to say: User can be complete without a UserInformation, but a UserInformation is always incomplete when it doesn’t have a User.

Additionally, if I set the relation up on both sides, EF gets tripped up. And really, I don’t need to specify the User relation going back the other way as I’m always coming from the other direction.

I’m wondering if EF landed on the wrong default policy for the columns being created here. There might be a perfectly valid reason for it (let me know!), but my User object doesn’t need to store a foreign key for 1:1 relations that it has, tables that relate to my User almost always seem like a better spot to put the foreign key.

So, I get that I can customize this, but I’d hate to have to do this once for every 1:1 in my system. Is there maybe even just a flag I can flip to tell EF to put the foreign key on the related side, not the relating side?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ajcvickerscommented, Jan 9, 2017

@atrauzzi Are you saying you think this should be interpreted as 1:1 by default? If that is the case, then everything else you say follows on from that. However, it is also valid to interpret it as 1:*, which is what EF does. At that point there is no longer any choice where to co-locate the FK–it has to be on the dependent, and that has to be the entity with the navigation property.

1reaction
atrauzzicommented, Jan 10, 2017

@ajcvickers - That doesn’t change or address any of my points about the default behaviour.

I know how it currently works, I know how the two approaches differ, that’s why I’m here.

Pretty sure I’ve been clear about this: I’m saying the way currently chosen is the less frequent and less desirable default. I do not agree at all that both approaches are effectively identical and solely down to preference.

I’ve already mentioned reasons why, and really - coming back with the same opinion seems more like a triage/discussion tactic than actually acknowledging the mistake or opportunity here.

(You can see my list of advantages above. The two approaches are not identical, so I hope laying that out here explicitly keeps people from talking past it any further.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Databases with Foreign Keys: Good way to picture forward ...
"Parent" vs "child" apply to any tree-structured or other hierarchically-structured relationship & "forward" vs "backward" are generic terms ...
Read more >
How to display backward or reverse relationship data in a ...
Hi, I can't figure out how to display backward relation data in my table. Here is a simplified version of my model structure:...
Read more >
Relationships in SQL - Complete Guide With Examples
Establish relations between the entities; Monitor logical relationships between tables; Reverse-engineer a database; Export database diagrams as ...
Read more >
Chapter 11 Functional Dependencies – Database Design
Decomposition is the reverse of the Union rule. If you have a table that appears to contain two entities that are determined by...
Read more >
Excel Magic Trick 1322: Backwards One To Many ... - YouTube
... ework backwards across a One To Many Relationship between Two Tables : 1. ... (24: 11 ) SQL coding in Access 6....
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