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;