Interpretation of actual project applications of ADO. NET Entity Framework in ASP. NET [reprint]

Source: Internet
Author: User
In ASP. NET, the actual project application of ADO. NET Entity Framework has doubts.

About ADO. NET Entity Framework Theory, entry-level teaching articles have been a lot of, but few people talk about ASP. net projects, which are easy to solve when learning.

For example, the most common method of using this form in the Tutorial:

Using (myobjectcontext c = new myobjectcontext ())

{

......

}

If such a code block is created every time a database needs to be connected in practical applications, several database connections may be created during access to a page, resulting in a large reduction in the number of concurrent user quotas;

If only one such code block is used, it is difficult to rub all database access operations into this code block due to layering, server controls, user controls, and other factors;

So what is the best solution?

This problem may be the most confusing issue for Entity Framework users.

Based on my experience, I will answer some questions that may arise in ASP. NET projects, including the above questions.

Preparations

First, we need to build a demo environment.

Create a database and create the following two tables:

Link: The postuser field of Article corresponds to the user ID field.

Fill two tables with some data:

Generate the entity framwork model:

 

The preparation is complete.

How to Design the business logic layer?

If you want to extend the functions of the object class, you should use the "partial Classification" (partial:

If you need to use the objectcontext object to query or update the database, you should define objectcontext as a method parameter instead of creating an objectcontext object in the business logic layer.

For example, the following method should not appear in the business logic layer:

 

A better approach is:

 

The highlighted overload method can only be written as needed.

(The Code statement for calculating the total number of statistics in the method can be completely replaced by the LINQ statement, which is very simple. Here is only a symbolic demonstration. don't worry too much)

How can I quickly obtain the corresponding object through the primary key?

In this way, the object is directly retrieved from the cache, which is very efficient.

How to Optimize Query performance?

You can use compiledquery. Compile () to pre-compile the query:

How to share an objectcontext on the same page?

Generally, only one database connection is required for a page, which reduces the database concurrency pressure and allows more users to access the website at the same time.

In general, we use the non-parameter constructor of objectcontext to create an objectcontext object containing the default connection. Therefore, share an objectcontext on the same page to share a database connection on the same page.

Next, we will place two entitydatasource controls on the page to obtain all users and all articles respectively and display them as listview controls.

You also need to place a user control to obtain and display the articles published by each user.

Let's first create a user control interface:

The User Control Interface contains only one listview control.

(For the sake of simplicity, the Li code in UL will be generated in the background and then directly bound here)

The following is the background code:

 

Note: An objectcontext attribute is defined here. The program will access the database and business logic layer through this attribute, but this attribute is not initialized here, instead, you need to initialize the property on the page that uses this user control.

Next, write the front-end code of the page:

Two entitydatasource and listview are simply placed and configured, and a user control created earlier is placed.

Background code:

An objectcontext attribute is also set here, And oninit is rewritten in the blue highlighted area, initialized in it, assigned to the user control, and then overwritten dispose in the green highlighted area, releases objectcontext.

In this way, the user control will use the same objectcontext object as the page.

However, each entitydatasource will also create its own objectcontext to connect to the database, which is obviously not our expectation. The solution is to register their contextcreating and contextdisposing events respectively and handle them as follows:

In this way, although the page accesses the database multiple times in different locations, it only uses one objectcontext and one database connection.

Execution result:

How to share a database connection in multiple objectcontext?

Entityconnection represents a database connection. You can create an entityconnection object and assign it to multiple objectcontext objects to achieve multi-objectcontext sharing single connection. For details, see msdn: http://msdn.microsoft.com/zh-cn/library/bb738582.aspx

Multiple objectcontext shares a connection. What is the difference between sharing an objectcontext with the whole page?

Generally, there is no difference, but when you commit the changes to the database, a single objectcontext will maintain all related transactions, and all will be rolled back once the commit fails, multiple objectcontext can process their respective transactions separately, which is more flexible than others.

Conclusion

Here I have summarized some problems and solutions based on my own experience. If you have any other questions, please add them and discuss them together.

If you have a better solution to the above problems, please kindly advise.

 

Download sample source code:

 

Download the XPS version of this article:

About ADO. NET Entity Framework Theory, entry-level teaching articles have been a lot of, but few people talk about ASP. net projects, which are easy to solve when learning.

For example, the most common method of using this form in the Tutorial:

Using (myobjectcontext c = new myobjectcontext ())

{

......

}

If such a code block is created every time a database needs to be connected in practical applications, several database connections may be created during access to a page, resulting in a large reduction in the number of concurrent user quotas;

If only one such code block is used, it is difficult to rub all database access operations into this code block due to layering, server controls, user controls, and other factors;

So what is the best solution?

This problem may be the most confusing issue for Entity Framework users.

Based on my experience, I will answer some questions that may arise in ASP. NET projects, including the above questions.

Preparations

First, we need to build a demo environment.

Create a database and create the following two tables:

Link: The postuser field of Article corresponds to the user ID field.

Fill two tables with some data:

Generate the entity framwork model:

 

The preparation is complete.

How to Design the business logic layer?

If you want to extend the functions of the object class, you should use the "partial Classification" (partial:

If you need to use the objectcontext object to query or update the database, you should define objectcontext as a method parameter instead of creating an objectcontext object in the business logic layer.

For example, the following method should not appear in the business logic layer:

 

A better approach is:

 

The highlighted overload method can only be written as needed.

(The Code statement for calculating the total number of statistics in the method can be completely replaced by the LINQ statement, which is very simple. Here is only a symbolic demonstration. don't worry too much)

How can I quickly obtain the corresponding object through the primary key?

In this way, the object is directly retrieved from the cache, which is very efficient.

How to Optimize Query performance?

You can use compiledquery. Compile () to pre-compile the query:

How to share an objectcontext on the same page?

Generally, only one database connection is required for a page, which reduces the database concurrency pressure and allows more users to access the website at the same time.

In general, we use the non-parameter constructor of objectcontext to create an objectcontext object containing the default connection. Therefore, share an objectcontext on the same page to share a database connection on the same page.

Next, we will place two entitydatasource controls on the page to obtain all users and all articles respectively and display them as listview controls.

You also need to place a user control to obtain and display the articles published by each user.

Let's first create a user control interface:

The User Control Interface contains only one listview control.

(For the sake of simplicity, the Li code in UL will be generated in the background and then directly bound here)

The following is the background code:

 

Note: An objectcontext attribute is defined here. The program will access the database and business logic layer through this attribute, but this attribute is not initialized here, instead, you need to initialize the property on the page that uses this user control.

Next, write the front-end code of the page:

Two entitydatasource and listview are simply placed and configured, and a user control created earlier is placed.

Background code:

An objectcontext attribute is also set here, And oninit is rewritten in the blue highlighted area, initialized in it, assigned to the user control, and then overwritten dispose in the green highlighted area, releases objectcontext.

In this way, the user control will use the same objectcontext object as the page.

However, each entitydatasource will also create its own objectcontext to connect to the database, which is obviously not our expectation. The solution is to register their contextcreating and contextdisposing events respectively and handle them as follows:

In this way, although the page accesses the database multiple times in different locations, it only uses one objectcontext and one database connection.

Execution result:

How to share a database connection in multiple objectcontext?

Entityconnection represents a database connection. You can create an entityconnection object and assign it to multiple objectcontext objects to achieve multi-objectcontext sharing single connection. For details, see msdn: http://msdn.microsoft.com/zh-cn/library/bb738582.aspx

Multiple objectcontext shares a connection. What is the difference between sharing an objectcontext with the whole page?

Generally, there is no difference, but when you commit the changes to the database, a single objectcontext will maintain all related transactions, and all will be rolled back once the commit fails, multiple objectcontext can process their respective transactions separately, which is more flexible than others.

Conclusion

Here I have summarized some problems and solutions based on my own experience. If you have any other questions, please add them and discuss them together.

If you have a better solution to the above problems, please kindly advise.

 

Download sample source code:

 

Download the XPS version of this article:

Related Article

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.