Summary of Entity Framework Object Relationships: one-to-one, one-to-least, role-to-least

Source: Internet
Author: User

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 " );
}
);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.