开发者

Linq update with explicit operator

开发者 https://www.devze.com 2022-12-08 03:49 出处:网络
I am trying to do an update with linq using an explict cast and the changes arent submitting. Here is the code

I am trying to do an update with linq using an explict cast and the changes arent submitting.

Here is the code

Image update = db.Images.Where(i =&开发者_StackOverflow社区gt; i.ImageId == imageWithChanges.ImageId).SingleOrDefault();

update = (Image)imageWithChanges;

db.SubmitChanges();

I have an explicit operator in my image class. Can anyone help?

Thanks


The line

update = (Image)imageWithChanges;

is not changing anything. It's merely swapping the thing the variable update points at. If you want to actually change the image, you'd probably have to copy each property from imageWithChanges to update.

Another way you can do this, is to attach imageWithChanges to db.Images and say it was a modified instance:

db.Images.Attach((Image)imageWithChanges, true); // true means "it's modified"
db.SaveChanges();


You say you got it fixed, but don't tell How.

For all others that will read this, I agree with Ruben, you have to Attach it. The error it gives you is valid, you have to either handle concurrency checking (with timestamp or version number) or let last in wins (by setting UpdateCheck to false for all your entity's properties).

0

精彩评论

暂无评论...
验证码 换一张
取 消