Entity framework4.0 (1) Overview (EF4 database first method)

Source: Internet
Author: User

Entity framework4.0 (hereinafter referred to as EF4) is a Microsoft ORM (Object-relation-mapping) framework. Like other ORM (such as nhib.pdf and hibernate), the first objective is to allow developers to operate relational data tables as objects. Second, to shield the databases of different underlying vendors, developers write CRUD (create, retrieve, update, delete) operations for data in the ORM framework, then, the orm framework translates these operations into dialects of different database vendors.

EF4 has greatly improved compared with earlier versions:

  • Poco (plain old CLR objects) supports persistence, tracking, and State management of business objects with business logic.
  • Model-driven development: EF4 provides three solutions: (1) Create a database and a data table first, and generate a business model from the tables in the database. (2) first design the business model with the designer and generate data tables from the business model. (3) the code-only method does not require a designer, but implements interfaces and classes on its own for ing with databases. The model driver here refers to the solution (2 ).
  • Delayed loading of correlated objects: in previous versions, the associated objects cannot be automatically loaded using navigation properties. You can use include () or display load. In EF4, related objects can be automatically loaded using navigation attributes.
  • Function-based calling of database stored procedures and user-defined functions: stored procedures and user-defined functions in a database can be mapped to objectcontext objects and called directly in a program.
  • Custom Code Generation template and generation process: the combination of EF4 and T4 can control the template for generating code. The combination of EF4 and WF (Windows work flow) can control the process of code generation.
  • By default, the entity set uses the plural number, and the entity uses the singular name form: in previous versions, the entity set has the same name as the entity, which makes people feel confused. EF4 has fixed this bug.
  • Complex attribute: If a property has only one item, we call it a scalar property ). If an attribute is composed of multiple scalar attributes, the attributes after the combination are called complex attributes. If an object has complex attributes. When mapped to a data table, each scalar attribute in the complex attribute is mapped into an independent field.

In the "Overview" section, I will use three blog posts to briefly demonstrate how to use EF4 to create an application. The goal is to show you the overall image of EF4 first, so as to avoid focusing too much on the details, but cannot see the full picture. I have seen a lot of blog posts about EF4, which are very good. However, I think it is a bit too deep, and the knowledge point is too steep: it is not conducive to new EF4 understanding and learning. There are no EF4 books in China for your reference. Therefore, I will try to write some simple and transitional blog posts that are useful to everyone. Avoid obscure technical terms, so that you can get started quickly and study them in depth.

EF4 supports three construction methods: 1. Database first. 2. model first method. 3. Code first method. Developers can select any method based on specific project conditions. In order to keep the detailed steps of each method coherent, the first three blog posts will not be explained in depth. In the subsequent sections, I will gradually expand and deepen EF4's internal mechanism and related knowledge.

Okay, I will not talk about it anymore. Let's get down to the truth. This is a simple demonstration:1. Database first method.

========================================================== ======================================

Let's create a simple example of Windows form: Take the northwind database as an example.

First, create efdemo windform application. For example:

Right-click the efdemo project and choose add-> new item. Select ADO. NET Entity Data model. In the Name box, enter: northwind and click Add.

Select "generate from database" and click Next. For example:

Select the database server and data table. Everyone should be familiar with this. A connection string (used to connect to the database) is generated and saved to the configuration file. For example:

For the tables, select categories and products. Check: pluralize or singularize generated object names and include foreign key columns in model.

You can set the namespace by yourself. Here we use the default value. Click Finish. For example:

The entity graph generated by EF4 is as follows:

The winform is designed as follows:

After running, click the: initlistbox button to fill in lboxcategory. When you select lboxcategory, all products under this category are displayed in lboxproduct. For example:

 

In the following two event processing codes, if either method 1 or method 2 is used. I recommend using method 2 for the two methods because it is more efficient. The first method is to demonstrate the use of join.

The click code in initlixtbox is as follows:

 

Button#click

1 // Method 1: We traverse the collection elements of the query results cyclically and add them to the control.
2 // This. lboxcategory. Items. Clear ();
3 // using (northwindentities context = new northwindentities ())
4 //{
5 // var categories = from category in context. Categories
6 // select new {category. categoryid, category. categoryname };
7
8 // foreach (var c in categories)
9 //{
10 // This. lboxcategory. Items. Add (C. categoryname );
11 //}
12 //}
13
14 // Method 2: This method specifies the data source. This. lboxcategory. Items. Clear () is not required ();
15 // to clear the last displayed result. After the data source is specified again, the control displays the latest data information.
16 using (northwindentities context = new northwindentities ())
17 {
18 var categories = from category in context. Categories
19 select new {category. categoryid, category. categoryname };
20
21 // Note: When you specify a data source for the control, the value assignment statement of datasource must be after the values of displaymember and valuemember,
22 // otherwise, the values of displaymember and valuemember do not take effect.
23 This. lboxcategory. displaymember = "categoryname ";
24 This. lboxcategory. valuemember = "categoryid ";
25 this. lboxcategory. datasource = categories;
26
27}

 

The select index change event response code of lbcategory is as follows:

Lboxcategory_selectedindexchanged

1 // Method 1: We traverse the collection elements of the query results cyclically and add them to the control.
2 // This. lboxproduct. Items. Clear ();
3 // If (this. lboxcategory. selecteditem! = NULL)
4 //{
5 // string categoryname = This. lboxcategory. selecteditem. tostring ();
6 // use categoryname directly for filtering this time. Join is required because the product only contains categoryid but does not have categoryname.
7 // using (northwindentities context = new northwindentities ())
8 //{
9 // var products = from product in context. Products
10 // join category in context. Categories on product. categoryid equals category. categoryid
11 // where category. categoryname = categoryname
12 // select new {product. productname };
13
14 // foreach (var p in products)
15 //{
16 // This. lboxproduct. Items. Add (P. productname );
17 //}
18 //}
19 //}
20
21 // Method 2: This method specifies the data source. This. lboxproduct. Items. Clear () is not required ();
22 if (this. lboxcategory. selectedvalue! = NULL)
23 {
24 // obtain the category ID
25 int categoryid = convert. toint32 (this. lboxcategory. selectedvalue. tostring ());
26
27 // This Time, categoryid is directly used as the filtering condition, and join is not required because the product contains categoryid.
28 using (northwindentities context = new northwindentities ())
29 {
30 var products = from product in context. Products
31 Where product. categoryid = categoryid
32 select new {product. productname };
33
34 // Note: When you control the specified data source, the value assignment statement for datasource must be after the values of displaymember and valuemember,
35 // otherwise, the values of displaymember and valuemember do not take effect.
36 This. lboxproduct. displaymember = "productname ";
37 This. lboxproduct. datasource = products;
38}
39}

 

In a brief introduction, EF4. What should I do first? I can't help it anymore. I have to rest...

 

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.