我正在尝试编写一个EF命令,如果在SaveChanges()上获得异常,它将修复其中一个字段。请参考以下代码示例:
model.IMAGES.AddObject(new IMAGES
{
IMAGE = imageByteArray,
IMAGE_NAME = pNum + "_OTHER.JPG",
VIEWPOINT_ID = 279,
ID_NO = ++id,
});
try
{
model.SaveChanges();
Trace.WriteLine("Saved: " + pNum + "_OTHER.JPG");
}
catch
{
model.REF_PART_IMAGES.Last().VIEWPOINT_ID = 272;
model.SaveChanges();
Trace.WriteLine("Saved: " + pNum + "_OTHER.JPG");
}
对Last()的调用抛出" LINQ to Entities无法识别方法' PullPics.IMAGES LastIMAGES'方法,并且此方法无法转换为商店表达式。"
有没有人有关于如何访问新创建的对象以进行修改的想法?
答案 0 :(得分:0)
我想通了,我不想要Last(),我只想保留对原始对象的引用并更新:
var image = new IMAGES
{
IMAGE = imageByteArray,
IMAGE_NAME = pNum + "_OTHER.JPG",
VIEWPOINT_ID = 279,
ID_NO = ++id,
};
model.IMAGES.AddObject(image);
try
{
model.SaveChanges();
Trace.WriteLine("Saved: " + pNum + "_OTHER.JPG");
}
catch
{
image.VIEWPOINT_ID = 272;
model.SaveChanges();
Trace.WriteLine("Saved: " + pNum + "_OTHER.JPG");
}