Entity Framework Comprehensive Tutorial Detailed

Source: Internet
Author: User

This document is mainly introduced. NET development of two new technologies,. NET Platform Language Integration query technology-LINQ, and ado.net new data access layer design technology ado.net Entity Framework. Ado. NET's LINQ to entity section is based on LINQ, First introducing LINQ technology for completeness. Preliminary Knowledge LINQ Technology

LINQ is a new technology in. NET 3.5, and this technology has expanded. NET platform, so that it can be more convenient for data query, simple LINQ technology mainly completes the collection objects (such as System.Collection or System.Collection.Generic under the Namespace object) query. Combining LINQ provider enables the operation of objects such as XML files (classes that are located under the System.Xml.Linq namespace using LINQ to xml–), and databases (which can use LINQ to SQL or LINQ to Entity, which are detailed below).

LINQ is a runtime-independent technology that runs on top of CLR2.0, and Microsoft extends the compiler for c#3.0 and VB9.0 so that it can compile LINQ-written programs into MSIL that can be understood by the CLR2.0 JIT.

Fundamentals of LINQ technology-c#3.0 Automatic Property Implicit type Object collection initializer anonymous class extension method lambda expression automatic properties

The concept is simple, and it simplifies what we're doing. NET when writing a bunch of private member + attribute programming way, we only need to declare a property by using the following way, the compiler will automatically generate the required member variables.

1 public class, Customer
2 {
3 public     int Id {get; set;}
4 public     string Name {get; set;}
5}

One of the purposes that I learned about the convenience of automatic properties in my project using LINQ is as follows:

In the process of fetching data using LINQ, we often need to use the Select New statement to query an object (often the IEnumerable type) for data binding. In general, if it is a direct binding (for example, to assign a query result directly to a DataSource property of a GridView control) we can directly select new to return an object of an anonymous class. If we still need to do further work on this collection object, we will have to return the object of a class using a language such as select New Class-name, in most cases only as a structure of the entity without having to do some operation. It would be very simple and efficient to use automatic properties to complete this class at this time. Implicit type

This name may be unfamiliar to you, but the keyword VAR should all be used, in C # when declaring an object with Var, the compiler automatically infers the type of the local variable based on its assignment statement. After the assignment, the type of the variable is determined and no further changes can be made. Additionally, the VAR keyword is also used for the declaration of anonymous classes.

Application: The main purpose of VAR is to represent the result of a LINQ query. This result may be an object of the objectquery<> or iqueryable<> type, or it may be an object of a simple entity type. Using VAR to declare this object can save a lot of time on code writing. object initializers and collection initializers

One way to construct an object in. NET2.0 is to provide an overloaded constructor, the second is to generate an object with the default constructor, and then assign its properties. In the. net3.5/c#3.0 we have a better way to initialize the object. That is using the object initializer. This feature is also a basis for anonymous classes, so it is introduced before anonymous classes.

Or that's the word, okay? The code is stronger than the annotation, and the following is a few snippets of code describing the initializer:

(Code from: Li Yongjing blog http://lyj.cnblogs.com)

Basic usage:

Nesting use:

1 User user = new user
 2 {
 3      Id = 1,
 4      Name = "Yjinglee",
 5 Age      = +,
 6 address      = new Address
 7      {
 8 city           = "Nanjing",
 9           Zip = 21000      }
11};

Like an object initializer Initializes an object, the collection initializer Initializes a collection, and in a word, you do not have to add the element individually by adding it through Add. Still give the code example:

Basic use:

With the object initializer, we can write the following concise code:

1 list<user> User = new list<user>{
2         new user{id=1,name= "Yjinglee", age=22},
3         new user{ Id=2,name= "Xieqing", age=25},
4};

Application occasions:

Or the Select New Class-name syntax mentioned earlier, you can directly follow an initializer to return the query results to this object. Anonymous Class

With the introduction of the previous initialization, anonymous classes are simple. We can initialize an anonymous class or an array of indeterminate types using the new {object initializer} or New[]{object, ...}. Objects of an anonymous class need to be declared with the VAR keyword. Sample code:

Application occasions:

Or, as mentioned in the previous example, the syntax for directly using select New {object initializer} is to return the result of a LINQ query to an anonymous class. extension Methods

Extension methods are one of the most important features that are added to C #. It plays a key role in the implementation of LINQ. There is no LINQ in the. NET2.0 era, so. NET2.0 and the collection classes in previous versions did not have a method reserved for LINQ at design time. Extension methods are needed in order to add LINQ support to this class without disrupting the existing encapsulation.

Extension methods use similar static methods, but in essence they are instance methods. This is because. NET3.5 's operating environment is still CLR2.0 so the language is unlikely to make great changes, all of which are grammatical sugars.

The following code is still used to illustrate the implementation of the extension method:

(Code from: Li Yongjing http://lyj.cnblogs.com)

1 public static class Extensions
2 {
3 public     Static Boolean isvalidemailaddress (this string s)
4     {
5         Regex regex = new Regex (@ "^[\w-\.] +@ ([\w-]+\.) +[\w-]{2,4}$ ");
6 return         regex. IsMatch (s);
7     }
8}

As shown in the code above, the extension method is a static method, declared in a static class, preceded by a "This" keyword, and the type of the argument represents the extension method to extend the type. The above code indicates that it is going to extend the string type.

Called by a static method that extends the method on the application as its extended type. As follows:

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.