Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to update data directly by EF

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how EF updates data directly, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.

A proxy wrapper class object is created in EF for each managed entity object, which tracks the state of the entity object and the state of each attribute.

1. EF update is usually used to query the data to be modified first, and then modify the new value; the corresponding attribute state of the modified entity object in the proxy wrapper class object will be modified and recorded. When SaveChanges is called, EF will traverse each entity object it manages, and according to the state of its wrapper class object, generate and execute the add, delete, change and check sql statement.

The modification operation in this example generates a modified sql statement (note: only the sql statement is generated for the modified attributes here), and finally executed.

Disadvantages: you have to query before you modify it, which is uncomfortable.

/ /-

/ / 1. First query the original data to be modified

Models.BlogArticle modelNew = db.BlogArticles.Where (a > a.AId = = model.AId) .FirstOrDefault ()

/ / 2. Set the modified value

ModelNew.ATitle = model.ATitle

ModelNew.AContent = model.AContent

ModelNew.ACate = model.ACate

/ / 3. Follow the newcomer to the database

Db.SaveChanges ()

Second, in order to avoid querying the database first, you can directly add the modified entity object to the EF for management (this is the additional state Attached), and manually set it to the unmodified state (Unchanged), and set the corresponding attribute of the modified entity object to the modified state.

Advantages: there is no need to query the database before modification.

/ /-

/ / 0.0 create a modified solid object

Models.BlogArticle model = new BlogArticle ()

Model.AId = 12

Model.ATitle = "New data"

Model.AContent = "New data ~"

Add / / 0.1 to the EF management container and get the pseudo-wrapped class object of the entity object

DbEntityEntry entry = db.Entry (model)

/ / * * if you use Entry to attach entity objects to the data container, you need to manually set the status of the objects of the entity wrapper class to Unchanged**

/ / * this sentence is not required if you use Attach

Entry.State = System.Data.EntityState.Unchanged

/ / 0.2 identifies that some properties of the entity object have been modified

Entry.Property ("ATitle") .IsModified = true

Entry.Property ("AContent") .IsModified = true

/ / 3. Follow the newcomer to the database

Db.SaveChanges ()

Thank you for reading this article carefully. I hope the article "how to update data directly from EF" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report