views:

812

answers:

2

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

I've accomplished this but only by knowing all of the entities properties and then using the "attachto" method.

My issues is i don't want my app to need to remember all of 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()

That entity also has a "created" field on it. I don't want to pass this "created" date all the way through my n-tier app. How can i just "not update" that field when saving to the database? Thanks! Paul

A: 

I don't think that it is possible to update an entity using save changes, without first loading it from the database. The code that 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 only update spesified fileds.

Shiraz Bhaiji
Can i do this through Linq to Sql instead?
Paul Lemke
I have not used Linq to SQL, but I think that you will run into the same problem.
Shiraz Bhaiji
+2  A: 

I figured it out, basically you use a stub instead, attach it, then only set the props you want to be updated. The entity framework will only update the things 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.

Paul Lemke