Net 4 Entity Framework new query and optimization [reprinted]

Source: Internet
Author: User
Net 4 Entity Framework new query and Optimization

Foreign key support (Foreign keys)

Added external key support for Entity Framework. With foreign key Association, you can now include foreign key attributes in the entity, which simplifies the development of key solutions such as data binding and N-tier development. You can directly use the foreign key property to set the relationship between objects:

Using (blogentities CTX = new blogentities ()){

Post mypost = new post {

Postid = 102,

Postname = "post title ",

Createddate = datetime. Now,

Postcontent = "post content ",

Blogid = 11

};

CTX. Posts. addobject (mypost );

CTX. savechanges ();

}

 

In this example, even if the blog object blogid = 11 has never been loaded, we can directly associate the newly created mypost object with this blog object through a foreign key.

Lazy Loading)

Entity
Framework supports delayed loading. When a new model is created in vs2010, entities that provide the delayed loading function are also generated. This function is enabled by default.
The object returned by the query operation will not be loaded immediately, but will be loaded when actually used. For example, delayed loading means that in the following code segment, each post object will be called
It is loaded when the postname attribute is printed.

Using (var ctx = new blogentities ()){
Foreach (var B in CTX. Blogs ){
Console. writeline (B. blogname );
// Note that the posts of the current blog is not loaded.
// EF will delay loading for us
Foreach (var p in B. Posts)
Console. writeline (P. postname );
}
}

 

 

Support for generation of SQL statement like

In EF4, add the support for using the database wildcard for string parameters of the where statement. For example, the following LINQ query statement is translated into a where clause and
Like statement, and use the '%' wildcard to search all blogs for a blog whose blogname attribute starts with 'visual studio.

VaR query = from B in CTX. Blogs
Where B. blogname. startswith ("Visual Studio ")
Select B;

 

Support for SQL statement in generation

In EF4, support for querying multiple values of the where statement parameter is added. For example, the following LINQ query statement is translated into a where clause and in statement, search all data whose blogname attribute is "visual" and "Studio" in all blogs using an array.

VaR query = from B in CTX. Blogs
Where new string [] {"visual", "Studio"}. Contains (B. blogname)
Select B;

 

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.