Entity Framework 7 One To One Code First Convention Idea
vsfeedback opened this issue · comments
This issue has been moved from a ticket on Developer Community.
Since you have improved EF conventions, I feel you have a chance to reduce the number confusing Relationship conventions for EF Code First.
I'm a SQL and C# expert, yet I find EF conventions sometimes unintuitive. Most of it is great but a few basic things got me to accidentally introduce a bug and actually got me fired.
Maybe you have a good reason for the conventions you chose, but if it was just arbitrary to get the work done then perhaps you want to take my suggestion.
To prevent breaking to many things, It would be good to have a flag to turn on to switch EF to use a more simple convention.
There are only 3 possible combinations:
a) 1 to 1
b) 1 to Many (Many to 1 is the same as 1 to Many but in reverse.)
c) Many to Many.
A 1 relationship should represented by a Reference Navigation Property.
A Many relationship should represent Collection Navigation Property.
That would mean you only need to have the following 3 combinations.
a) 1 to 1: Reference Navigation Property to Reference Navigation Property
b) 1 to Many: Reference Navigation Property to Collection Navigation Property
c) Many to Many: Collection Navigation Property to Collection Navigation Property
For Example:
a) To have a 1 to 1 relationship between Entity1 and Entity2 you would have:
Entity1 with Reference Navigation Property to Entity2
and
Entity2 with Reference Navigation Property to Entity1
public class Parent
{
public int ParentId { get; set; }
public string Name { get; set; }
public Child Child { get; set; }
}
public class Child
{
public int ChildId { get; set; }
public string Name { get; set; }
public Parent Parent{ get; set; }
}
b) To have a 1 to Many relationship between Entity1 and Entity2 you would have:
Entity1 with Collection Navigation Property to Entity2
and
Entity2 with Reference Navigation Property to Entity1
public class Parent
{
public int ParentId { get; set; }
public string Name { get; set; }
public ICollection Kids { get; set; }
}
public class Child
{
public int ChildId { get; set; }
public string Name { get; set; }
public Parent Parent{ get; set; }
}
c) To have a Many to Many relationship between Entity1 and Entity2 you would have:
Entity1 with Collection Navigation Property to Entity2
and
Entity2 with Collection Navigation Property to Entity1
public class Parent
{
public int ParentId { get; set; }
public string Name { get; set; }
public ICollection Kids { get; set; }
}
public class Child
{
public int ChildId { get; set; }
public string Name { get; set; }
public ICollection Parent{ get; set; }
}
Those are the base cases that your engineers can revisit because they are the root of the confusion.
From there, you can reuse all the great work for specifying foreign key field names and optional null relationships such as
public class Parent
{
public int ParentId { get; set; }
public string Name { get; set; }
public int? OnlyChildId { get; set; }
public Child OnlyChild { get; set; }
}
public class Child
{
public int ChildId { get; set; }
public string Name { get; set; }
public int? SingleParentId { get; set; } //Possible Orphans
public Parent SingleParent{ get; set; }
}
Original Comments
Feedback Bot on 6/8/2023, 03:59 AM:
(private comment, text removed)
This issue has been closed because EF6 is no longer being actively developed. We are instead focusing on stability of the codebase, which means we will only make changes to address security issues. See the repo README for more information.