Series of entiy Framework practicesArticleThe entity relationship of the Entity Framework.
Why write articles to clarify these relationships? When I first started using the Entity Framework, I had to take some difficult detours because I was not calm down to clarify the relationship. Currently, our projects are all using the Entity Framework, so as to prevent others from having to go through a "pain-free" detour. So I made up my mind to "clarify the relationship" and "write a blog ". However, writing a blog can help you solve the problem completely and avoid the problem from being abandoned. When I write these articles, I have a deeper understanding of the problem.
Let's learn from each other. Through this summary, we will take a hot dive into our understanding of the relationship between EF entities, which may lead to new gains. The same is true for our feelings. When feelings get cold, don't forget to go back to the hot pot.
1. One-to-one relationship)
A) one-to-one (article link)
Class diagram:
Database table structure:
Definition of Entity Relationship in Entity Framework:
Modelbuilder. Entity<Blogsite>()
. Hasrequired (B=>B. bloguser)
. Withtasks ()
. Hasforeignkey (B=>B. userid );
B) bidirectional one-to-one (article link)
Class diagram:
Database table structure:
Definition of Entity Relationship in Entity Framework:
Modelbuilder. Entity<Blogsite>()
. Hasrequired (B=>B. bloguser)
. Withtasks ()
. Hasforeignkey (B=>B. userid );
Modelbuilder. Entity<Bloguser>()
. Hasrequired (u=>U. blogsite)
. Withtasks ()
. Hasforeignkey (u=>U. blogid );
2. one-to-many relationship (article link)
Class diagram:
Database table structure:
Definition of Entity Relationship in Entity Framework:
Modelbuilder. Entity<Blogsite>()
. Hasmany (B=>B. blogposts)
. Withrequired (P=>P. blogsite );
3. Multi-to-many relationship (article link)
Class diagram:
Database table structure:
Definition of Entity Relationship in Entity Framework:
Modelbuilder. Entity < Blogpost > ()
. Hasmany (B => B. Categories)
. Withtasks (C => C. blogposts)
. Map
(
M =>
{
M. mapleftkey ( " Blogpostid " );
M. maprightkey ( " Categoryid " );
M. totable ( " Blogpost_category " );
}
);