ADO.net Entity Framework: Update only certian properties for a single object

I want to update an object without first loading the object from the database.

I did this, but only knowing all the properties of entities, and then using the "attachto" method.

My problems: I do not want my application to need to remember all the properties. Example:

Dim customerEntitiy As New shopper customerEntitiy.shopper_id = CustomerData.CustomerID customerEntitiy.market_code = CustomerData.MarketSector customerEntitiy.email = CustomerData.Email customerEntitiy.modified = DateTime.Now context.AttachTo("shopper", customerEntitiy) context.SaveChanges() 

This object also has a "created" field. I do not want to pass this "created" date completely through my n-level application. How can I just “not update” this field when saving to the database? Thank you Floor

+1
entity-framework
source share
3 answers

I figured this out, basically you use a stub instead, attach it, and then set only the details you want to update. An entity structure will only update things that have changed.

 Dim customerEntitiy As New commerce_shopper customerEntitiy.shopper_id = CustomerData.CustomerID 'this is the primary key' context.AttachTo("commerce_shopper", customerEntitiy) customerEntitiy.market_code = CustomerData.MarketSector customerEntitiy.email = CustomerData.Email customerEntitiy.modified = DateTime.Now context.SaveChanges() 

This bypasses the "created" date field.

+3
source share

I do not think that it is possible to update an entity using save changes without first loading from the database. The code you have will generate an insert statement, not an update.

You may be able to accomplish what you are trying to do using stored procedures that update only created files.

0
source share

All Articles