Entity Framework practice series-relationship-Child Care (one-to-many)

Source: Internet
Author: User

From Acacia (one-to-one), to mutual affection (two-to-one), and into the marriage hall, the next step is to have children, men are upgraded to fathers, if many children are given birth, the relationship between the father and the child is "one-to-many ".

A father has multiple children, and a child belongs to only one father.

Let's take a blog as an example. The scenario is as follows:

One blog (blogsite) has multipleArticle(Blogpost), an article only belongs to one blog.

View the class diagram:

View table structure:

Definitions of blogsite and blogpostCode:

 Public     Class  Blogsite
{
Public Int Blogid { Get ; Set ;}
Public String Blogapp { Get ; Set ;}
Public Bool Isactive { Get ; Set ;}
Public Guid userid { Get ; Set ;}
Public Virtual Bloguser { Get ; Set ;}
Public Virtual Icollection < Blogpost > Blogposts { Get ; Set ;}
}

 Public     Class  Blogpost
{
Public Int Id { Get ; Set ;}
Public String Title { Get ; Set ;}
Public Int Blogid { Get ; Set ;}
Public Blogsite { Get ; Set ;}
}

The following is a key step. In onmodelcreating of Entity Framework, use the fluent API to define the "one-to-many" relationship:

Modelbuilder. Entity<Blogsite>()
. Hasmany (B=>B. blogposts)
. Withrequired (P=>P. blogsite );

The code is simple and intuitive. It is a blog has#, an article require, and a blog.

Next we will verify it through three query scenarios.

Scenario 1: Enter a blog (blogsite) and read all the articles in the blog (blogpost ).

Code for querying the data using LINQ:

PublicBlogsite getblogsite (IntBlogid)
{
Return_ Blogsitereposiotry. Entities
. Include (B=>B. blogposts)
. Firstordefault (B=>B. blogid=Blogid );
}

Test code:

 [Testmethod]
Public Void Getblogsite_test ()
{
VaR blogsite = _ Aggblogsiteservice. getblogsite ( 1 );
Assert. isnotnull (blogsite );
Console. writeline ( " Blogapp: " + Blogsite. blogapp );
Console. writeline ( " Blogposts: " );
Blogsite. blogposts. tolist (). foreach
(
P => Console. writeline (P. Title + " - " + P. blogsite. blogapp)
);
}

Test results:

Actually executed SQL:

This section of SQL seems complicated. In fact, SQL Server will optimize it during execution and you will know the execution plan.

Pass the test!

Scenario 2: read an article (blogpost) and know which blog (blogsite) is from)

Code for querying the data using LINQ:

PublicBlogpost getblogpost (IntBlogpostid)
{
Return_ Blogpostrepository. Entities
. Include (P=>P. blogsite)
. Firstordefault (P=>P. ID=Blogpostid );
}

Test code:

 [Testmethod]
Public Void Getblogpost_test ()
{
VaR P = _ Aggblogsiteservice. getblogpost ( 1 );
Assert. isnotnull (P );
Console. writeline ( " Blogpost title: " + P. Title + " , " +
" Blogapp: " + P. blogsite. blogapp );
}

Test results:

Actually executed SQL:

Test passed!

Scenario 3: read a lot of articles (blogpost) and know the blog from which each article comes from (blogsite)

Code for querying the data using LINQ:

PublicIenumerable<Blogpost>Getallblogposts ()
{
Return_ Blogpostrepository. Entities
. Include (P=>P. blogsite );
}

Test code:

 [Testmethod]
Public Void Getblogposts_test ()
{
_ Aggblogsiteservice. getallblogposts (). tolist ()
. Foreach (
P => Console. writeline ( " Blogpost title: " + P. Title + " , " +
" Blogapp: " + P. blogsite. blogapp)
);
}

Test results:

Actually executed SQL:

Easy to pass!

Summary

Haswon + withrequired: "one-to-many" relationships are easy to understand, simple to implement, and minimal detours.

Everything becomes simple as long as you are happy with each other, and the most difficult thing is how to change from "lovesickness" to "mutual affection ".

 
Haswon

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.