This article is a mirror article of machine translation, please click here to jump to the original article.

View: 16557|Reply: 0

[ASP.NET] Asp.Net MVC native EF insertion, the correct way to update the data

[Copy link]
Posted on 10/15/2018 11:16:53 AM | | |
introduction
EF is the official ORM framework recommended by Dapper and NHibernate, and its convenience and speed in the development process are undoubted, but due to some caching mechanisms and tracking mechanisms of EF itself, there are some places that need special attention when using it.

Below I will share some of my experiences in the project, hoping to help you use EF correctly.

1. Insert data
What is not recommended:

Correct approach:

. SaveChanges() returns an Int value for the number of affected bars

2. Update data
What is not recommended:

Note: This practice must be queried again by passing the primary key in to prevent it from being added to the . AsNoTracking() method is mixed, . AsNoTracking() works by giving up tracking of EF objects, so that after reassigning the property, the . SaveChanges() will be invalid.

Correct approach:

The above practice will inevitably lead to the need to set IsModified repeatedly if you want to change multiple fields.

We further make an encapsulation of the modification method, the encapsulation method is as follows:







Previous:Introduction to BonusCloud router mining
Next:. Net distributed architecture: Session sharing based on Redis
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com