New Problem New Method: update the specified field in Entity Framework]

Source: Internet
Author: User
New Problem New Method: In Entity
Update specified fields in framework

Another article may be annoying. However, writing a blog can improve yourself and help others. It is a great deal of money to gain in every write process and after publishing! Please endure it.

In the previous article, "prepare to use entity
In the framework to implement data on-demand Update ", we implement on-demand update, but it is different from the Application scenario of the specified field update here.

In the previous On-Demand update Application Scenario, data to be updated and not to be updated are provided to the Entity Framework, and EF determines which data needs to be updated.

The current application scenario is: I clearly know which field to update, so that EF only updates this operation, so you don't have to worry about anything else.

Next we will compare the differences between the two through a metaphor.

For example, I want to change some parts in a car repair shop. I parked my car in the garage and came to the repair room. At this time, I can no longer directly access this car, and I have to go through the repair room staff. The staff gave me a car model. I had any idea of changing parts and could only tell him through this model.

In the first on-demand update scenario, I don't know which parts need to be replaced, but I only know what to do after the change.

The process is:

1. Tell the staff the license plate number and let him create an identical model based on the car I put in the garage.

2. The staff handed the model to me.

3. I changed the model to what I wanted (but I don't know which parts should be changed or not ).

4. Hand over the modified car model to the employee and ask him to repair it.

5. I went shopping without worrying about anything.

In the second scenario, I want to change the headlights Based on the specified fields. In a car repair shop, you don't have to worry about whether the headlights are damaged. I just want to change the headlights so that you can make money.

The process is:

1. I took an empty car model and changed it to the headlights I wanted.

2. Hand over the car model to the staff and let him repair it.

3. I went shopping without worrying about anything.

For the second scenario, if we adopt the operation procedure of the first scenario, the staff will be tired, the efficiency will be low, and the cost will naturally be high. The second process is an inevitable choice.

Today, we finally found a solution for the second process.

For example, to update the last update time of a blog, we only need:

1. Create a new blog entity object and tell him the ID of the blog to be updated and the "Last Update Time ".

2. Submit the object to the Entity Framework to complete the update.

The Code is as follows:

Public void updateblogcoinfiglastupdatedtest ()
{
Using (blogdbcontext context = new blogdbcontext ())
{
VaR blog = new blog () {blogid = 0, lastmodified = datetime. Now };
Context. blogconfigs. Attach (blog );
VaR stateentry = (iobjectcontextadapter) Context). objectcontext.
Objectstatemanager. getobjectstateentry (blog );
Stateentry. setmodifiedproperty ("lastupdated ");
Context. savechanges ();
}
}

The SQL statement generated by EF is as follows:

Exec sp_executesql n' update [DBO]. [blog_config]
Set [lastupdated] = @ 0
Where ([blogid] = @ 1)
', N' @ 0 datetime2 (7), @ 1 int', @ 0 = '2017-04-06 14:12:28. 100', @ 1 = 0

The secret is hidden in objectcontext, And the objectcontext attribute of context cannot be obtained normally.

Fortunately, the heroes in the garden have circulated a secret to the rivers and lakes. With this secret, we have found a new way for this new problem. Thank you for choosing lingzhisun

Another article may be annoying. However, writing a blog can improve yourself and help others. It is a great deal of money to gain in every write process and after publishing! Please endure it.

In the previous article, "prepare to use entity
In the framework to implement data on-demand Update ", we implement on-demand update, but it is different from the Application scenario of the specified field update here.

In the previous On-Demand update Application Scenario, data to be updated and not to be updated are provided to the Entity Framework, and EF determines which data needs to be updated.

The current application scenario is: I clearly know which field to update, so that EF only updates this operation, so you don't have to worry about anything else.

Next we will compare the differences between the two through a metaphor.

For example, I want to change some parts in a car repair shop. I parked my car in the garage and came to the repair room. At this time, I can no longer directly access this car, and I have to go through the repair room staff. The staff gave me a car model. I had any idea of changing parts and could only tell him through this model.

In the first on-demand update scenario, I don't know which parts need to be replaced, but I only know what to do after the change.

The process is:

1. Tell the staff the license plate number and let him create an identical model based on the car I put in the garage.

2. The staff handed the model to me.

3. I changed the model to what I wanted (but I don't know which parts should be changed or not ).

4. Hand over the modified car model to the employee and ask him to repair it.

5. I went shopping without worrying about anything.

In the second scenario, I want to change the headlights Based on the specified fields. In a car repair shop, you don't have to worry about whether the headlights are damaged. I just want to change the headlights so that you can make money.

The process is:

1. I took an empty car model and changed it to the headlights I wanted.

2. Hand over the car model to the staff and let him repair it.

3. I went shopping without worrying about anything.

For the second scenario, if we adopt the operation procedure of the first scenario, the staff will be tired, the efficiency will be low, and the cost will naturally be high. The second process is an inevitable choice.

Today, we finally found a solution for the second process.

For example, to update the last update time of a blog, we only need:

1. Create a new blog entity object and tell him the ID of the blog to be updated and the "Last Update Time ".

2. Submit the object to the Entity Framework to complete the update.

The Code is as follows:

Public void updateblogcoinfiglastupdatedtest ()
{
Using (blogdbcontext context = new blogdbcontext ())
{
VaR blog = new blog () {blogid = 0, lastmodified = datetime. Now };
Context. blogconfigs. Attach (blog );
VaR stateentry = (iobjectcontextadapter) Context). objectcontext.
Objectstatemanager. getobjectstateentry (blog );
Stateentry. setmodifiedproperty ("lastupdated ");
Context. savechanges ();
}
}

The SQL statement generated by EF is as follows:

Exec sp_executesql n' update [DBO]. [blog_config]
Set [lastupdated] = @ 0
Where ([blogid] = @ 1)
', N' @ 0 datetime2 (7), @ 1 int', @ 0 = '2017-04-06 14:12:28. 100', @ 1 = 0

The secret is hidden in objectcontext, And the objectcontext attribute of context cannot be obtained normally.

Fortunately, the heroes in the garden have circulated a secret to the rivers and lakes. With this secret, we have found a new way for this new problem. Thank you for choosing lingzhisun

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.