Blog garden modernization-Prepare to use Entity Framework to update data on demand

Source: Internet
Author: User

Here, "On-Demand Update" refers to updating only the modified data. For example, when we open the casual editing page in the background of a blog, there is a lot of data that can be modified, but we only changed the casual title, and nothing else was changed, the database only updates the title field, which is updated on demand.

Currently, we use a very backward method. A stored procedure with many parameters, No matter what information you modify, even if you only change the title, all fields must be updated when they are saved.

The modernization of the blog Park is not just a slogan. Efforts must be made to change the backward areas.

One feature of Entity Framework showed us hope that it was worthwhile to introduce the modern equipment of Entity Framework.

The Entity Framework can track the changes in object attributes. Only modified attributes are updated when saved to the database. Below we useCodeTake a look at this feature of Entity Framework.

Object Class blogpost:

 Public     Class  Blogpost
{
Public Int Id { Get ; Set ;}
Public String Title { Get ; Set ;}
Public String Description { Get ; Set ;}
Public String Author { Get ; Set ;}
}

Blogdbcontext:

Public ClassBlogdbcontext: dbcontext
{
PublicDbset<Blogpost>Blogposts {Get;Set;}
}

Data Update operations:

 Blogpost post  =     New  Blogpost ()
{
ID = 3560 ,
Title = " Title Test " ,
Description = " Description Test " ,
Author = " Blog "
};
Using (Blogdbcontext Context = New Blogdbcontext ())
{
Context. blogposts. Attach (post );
Post. Title = " Title changed " ;
Post. Description = " Description Test " ;
Context. savechanges ();
}

This data update operation only updates the post title (only the title is modified). Let's take a look at the final SQL statement executed by Entity Framework:

ExecSp_executesql n'Update [DBO]. [blog_content]
Set [title] = @ 0
Where ([ID] = @ 1)
', N'@ 0 nvarchar (128), @ 1 int',@ 0=N'Title changed',@ 1=3560

Very powerful. Only the title field is updated.

However, there is always a big gap between ideal and reality. To apply this powerful feature to Web applicationsProgramAnd will face many challenges.

Let's take a look at the above data update code: to update the object before attach changes within the context lifecycle, modify the object and save it.

In web application scenarios, you cannot pass object to the client within the context lifecycle and modify the object on the client.

The actual application scenarios we need to deal with are as follows (ASP. net mvc + Ajax ):

1. the client browser first initiates a request to the server. The request obtains a page containing the original attribute value of the object to be modified. The server end is within the lifecycle of the context (blogdbcontext instance, complete a query operation.

2. Modify the property value in the browser of the client.

3. Send all attribute values (modified and unmodified) to the server in JSON format.

4. the server creates a blogpost instance -- modifiedblogpost (if there is no cache, to obtain the blogpost instance by querying the database, and then assign the JSON data to the corresponding attribute ).

5. In the lifecycle of the business logic layer in context (a new blogdbcontext instance:

A) Get the modified blogpost instance originalblogpost (if there is no cache, query from the database );

B) Attach (originalblogpost );

C) Copy all attribute values of modifiedblogpost to originalblogpost;

D) savechanges ();

Problems in this scenario (repeated operations ):

1. Operations on obtaining objects before modification in items 1 and 5..

2. Operations for assigning values to object attributes in item 4th and item 5. c.

Solution:

The JSON data sent from the client is directly transmitted to the business logic layer in the form of strings, and then the originalblogpost attribute assignment operation in the business logic layer based on JSON data is completed directly in 5.C.

This article records a piece of detail in the modernization process of the blog Park. Next, we will overcome this problem and continue to work hard...

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.