Entity Framework 4.0 recipes complex type in read notes 1 EDM

Source: Internet
Author: User

Before writing:

After six months of Java + Silverlight development, I felt very happy to go back to Asp.net development on the pure MS Platform. many other times have been spent on Silverlight, so this time I went back to Asp.net and made a book plan for myself. first of all, C #4.0 and my favorite Entity Framework 4.0

Fortunately, I got the Entity Framework 4.0 recipes book (English version). After more than a month of scum meeting, I felt that my English skills had improved a lot, from the beginning, I wrote about what I want to write every day, to thinking about what I want to talk about, organizing my work on a temporary basis, and now I am able to express some ideas on an impromptu basis, mainly to overcome the tension. However, what is still lacking is listening, which cannot be a good follow foreign team member. If you don't want to talk about it, go back to Entity Framework 4.0 recipes. After I saw chapter 2nd, I found that the example was familiar, and I found that it was written by zeeshan hirani (in EF beta and 1.0, I have read many blogs written by him and a 500-page EF study notes written by him.) No wonder this is so kind. Because I was familiar with EF before, the content of Reading Notes is mainly about the new feature part of my unfamiliar part and ef4.0, so I want to know more about EF4, or reading is comprehensive.

Several special relationships in EDM: Of course, the most important thing to learn is complex type, which is an important feature in ef4.0.

A. No load (modeling a packet-to-load relationship with no payload)

B. modeling a load-to-load relationship with payload)

For more information about the many-to-many relationship and problem, visit the blog I wrote in at http://www.cnblogs.com/subway-2008/archive/2008/08/20/1272375.html.

C. Self-reference (modeling a self-referencing relationship)

 

For example, in the left image picturecategory, the associated attribute is subcategories (*) and parentcategory (0... 1) there is no difference between addition, deletion, query, modification, and many-to-many relationships. If you want to traverse a node, you only need to use recursion.
Note: However, the Association in ef4.0 does not have a mapping step. Remember to set the mapping of Object Relations in ef1.0.

 

D. Map multiple tables to one object (splitting a entity into SS multiple table) 0...

 

 

The product and productwebinfo tables are the relationship between the primary and Foreign keys. The fields of the two tables are mapped to the same object respectively: eg

Using (VAR context = new efrecipesentities ())
{
VaR Product = new product {SKU = 147, description = "expandable hydration pack", price = 19.97 M, imageurl = "/pack147.jpg "};
Context. Products. addobject (product );
Product = new product {SKU = 178, description = "rugged Ranger duel bag", price = 39.97 M, imageurl = "/pack178.jpg "};
Context. Products. addobject (product );
Product = new product {SKU = 186, description = "range field pack", price = 98.97 M, imageurl = "/noimage.jp "};
Context. Products. addobject (product );
Product = new product {SKU = 202, description = "Small deployment back pack", price = 29.97 M, imageurl = "/pack202.jpg "};
Context. Products. addobject (product );

Context. savechanges ();
}

E. Map multiple objects to a table (splitting a table into SS multiple entities)

For example, the left graph map the bianry field of photograph to an object separately, which is conducive to improving the performance of a single table. Because of this idea, for multi-field tables, common fields are placed in one entity, and some fields that are not commonly used or have a large capacity (bianry type) are mapped to another entity. I remember that I also discussed this issue during ef1.0 and thought that using stored procedures was a good method. Eg:

Using (VAR context = new efrecipesentities ())
{
VaR photo = new photograph {photoid = 1, Title = "My dog", thumbnailbits = thumbbits };
VaR fullimage = new photographfullimage {photoid = 1, highresolutionbits = fullbits };
Photo. photographfullimage = fullimage;
Context. Photographs. addobject (photo );
Context. savechanges ();
}

 

F. Table inheritance

Hierarchy inheritance

This article was written in ef1.0.ArticleIn my eyes, inheritance is more comprehensive.

G. Is A and has a (molding is a and has a relationships between two entities) between entities)

Is A is an inheritance

Has a is the navigation Property

H.Complex Type) This is the new feature of ef4.0, which is not supported in version 1.0. Complex Type allows you to add several attributes to a type in the way of a family, and this type is used as an attribute of an object, complex type can contain scalar attributes and other complex types, but it cannot be used as a navigation attribute. Complex type cannot be an entity key. complex types is not tracked in object context (tracked)

Note: complex type cannot be empty. Therefore, if the value of complex typed is not very important for a specific operation, it is best to create a default value to prevent errors.

 

 

 

Operation Procedure: add the table agent to EDM to produce an agent object, select the lastname and firstname attributes, right-click refactored into complex type, and rename complextype1 as name in the Model Browser, rename the complex type property on the Agent object to name. similarly, addressline1, addressline2, city, state zipccode attributes refactored into the complex type address.

The above is the map.CodeHow to access complex type

using (VAR context = new efrecipesentities ()
{< br> var name1 = new name {firstname = "Robin", lastname = "Rosen "};
var name2 = new name {firstname = "Alex", lastname = "St. james "};
var address1 = new address {addressline1 =" 510 n. grant ", addressline2 =" apt. 8 ", city =" Raytown ", state =" Mo ", zipcode =" 64133 "};< br> var address2 = new address {addressline1 =" 222 Baker St. ", addressline2 =" apt.22b ", city =" Raytown ", state =" Mo ", zipcode =" 64133 "};

VaR nametest = new name ();

Context. Agents. addobject (new agent {name = name1, address = address1 });
Context. Agents. addobject (new agent {name = name2, address = address2 });
Context. savechanges ();
}

Using (VAR context = new efrecipesentities ())
{
Console. writeline ("agents ");
Foreach (VAR agent in context. Agents)
{
Console. writeline ("{0} {1}", agent. Name. firstname, agent. Name. lastname );
Console. writeline ("{0}", agent. Address. addressline1 );
Console. writeline ("{0}", agent. Address. addressline2 );
Console. writeline ("{0}, {1} {2}", agent. Address. City, agent. Address. State, agent. Address. zipcode );
Console. writeline ();
}
}

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.