Entity Frame Relationship

Source: Internet
Author: User

Entity Framework-clearing relationship-one-way one-to-one relationship based on foreign Key association

Note: This article is for the Entity Framework Code one scenario.

Previously wrote three articles trying to clarify the Entity framework of a one-to-one relationship (single love (one-way one-on-one), two lovers (two-way one-on-one), two lovers-continued), but was not clear enough, the new year to get back to the reason.

At that time "one-to-one" entity relationship, the corresponding database relationship is a foreign key association (in fact, a "one-to-many" relationship, so the mapping used Withmany). And the "one-to-one" relationship in the database is the shared primary key (this is my personal understanding, the wrong place, welcome to point out), the next article will be the reason for this relationship.

Because two-way "one-to-one" relationships are rarely used and are not recommended, for a clearer understanding, we only talk about one-way one-to-one relationships, which are "one-way one-to-one relationships based on foreign keys (one-to-one unidirectional relationships)", The corresponding previous article is Tansians (one-way one-to-one).

1. Class Diagram

2. Definition of Class

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 Bloguser {get; set;}
}
public class Bloguser
{
Public Guid UserID {get; set;}
public string Author {get; set;}
public int BlogID {get; set;}
}

3. Database structure

4. Enitity Framework Mapping Relationship definition

protected override void Onmodelcreating (Dbmodelbuilder modelbuilder)
{
Modelbuilder.entity<blogsite> ()
. Hasrequired (b => b.bloguser)
. Withmany ();
}

How to understand the hasrequired and Withmany here?

My understanding is: Hasrequired is aimed at the relationship between Blogsite and Bloguser, Withmany is the relationship between Bloguser and Blogsite. Hasrequired (b => b.bloguser). Withmany () indicates that there is a required association between Blogsite and Bloguser (one-to-one, each blogsite has a corresponding bloguser), This association is one-to-many for Bloguser (a bloguser can have multiple blogsite).

You may be wondering. Clearly is one-way one-to-one entity relations, here how to make a one-to-many relationship.

If there is such doubt, is normal phenomenon, I wrote the article from last July began to doubt, has been puzzled until now, write an article just a little understand.

Notice what "one-way one-on-one" is. is an entity relationship; What the Entity framework is. Is O/RM, is used to map the relationship between entity and database; what's missing. Database relationships.

The database structure in the diagram above shows that blogsite and bloguser are foreign key associations, and the following figure shows this more clearly.

This foreign key association represents a one-to-many database relationship between Bloguser and Blogsite.

To sum up:

A one-way one-to-one between entity relationships--blogsite and Bloguser

Database relationship between--bloguser and Blogsite One-to-many

Is that so? Does the Entity framework think so too? Let's try to verify.

How do you know the idea of the Entity Framework?

Through the EDM.

But this is code one.

No matter what the one, there is an EDM, because this is the map of the Entity framework, without which the Entity framework will be disoriented. The EDM for Code one is generated when the EF is running, not without maps, but in the heart of the Entity Framework, we can't see it.

How to let the Entity Framework to speak the truth.

From Master Morteza Manavi learned a trick, the code is as follows:

using (var context = new context ())
{
XmlWriterSettings settings = new XmlWriterSettings ();
Settings. Indent = true;

using (XmlWriter writer = xmlwriter.create (@ "MODEL.EDMX", Settings))
{
EDMXWRITER.WRITEEDMX (context, writer);
}
}

With the code above, you can get the map--edmx files in the EF heart. Please look at the map:

Sure enough, the heart of the EF map is bloguser and blogsite a one-to-many relationship. What the role of the map is. is to let EF find the corresponding data in the database through the map. How the map is produced. We told entity Framework:.hasrequired by Fluentapi (b => b.bloguser). Withmany ().

Let's dissect it again. Hasrequired (b => b.bloguser). Withmany ().

Before, I have been plagued by the fact that the definition here is always defined as the relationship of the entity. Wrong. Although this is defined by the entity, it defines the mapping between the entity and the data in the database (which is common sense and is ignored), and more so tells EF about the data relationships of these entities in the database. The EF ultimately generates the corresponding SQL based on this definition.

So let's take a look at the point of generating query sql:

. Hasrequired (b => b.bloguser) tells EF that this is a INNER join query (blogsite INNER join Bloguser), but the INNER join requires a condition, withmany () Tell EF This is a foreign key association, which the EF infers, finds the primary key userid from the Bloguser, and checks to see if there is a property named UserID in the Blogsite and, if so, queries it as a foreign key. And we have userid in our blogsite, so we generate the following SQL:







[Extent2]. [Author] As [Author]
From [dbo].[ Blogsite] as [Extent1]
INNER JOIN [dbo]. [Bloguser] As [Extent2] on [Extent1]. [UserID] = [Extent2]. [UserID]
1 = [Extent1]. [IsActive]


Http://www.cnblogs.com/dudu/archive/2012/01/04/entity_framework_one_to_one_unidirectional.html


. Hasrequired (a => a.b) says: 1 Entity A is a relationship to entity B, entity A has a navigation property a.b;2) There is a one-to-one association (INNER JOIN) between table A and table B in the database.

. Withmany () means: 1 entity B can have no relationship with entity A, or it can be a one-to-many relationship; 2 There is a foreign key association between table A and table B in the database.

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.