SaveChanges在未更改的对象上抛出UpdateException [英] SaveChanges throws UpdateException on unchanged objects

查看:98
本文介绍了SaveChanges在未更改的对象上抛出UpdateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我遇到了一个我没有理解的问题,我希望有人可以帮助我理解和修复它。


我正在使用C#与Entity Framework 4和自跟踪实体POCO层(使用T4模板)。我的上下文有LazyLoading = true 和ProxyCreationEnabled = false  (但我想我在那里尝试了所有选项)



我的数据库模式如下:

         活动 - >项目 - > 客户 - >地址   


因此,活动具有对项目的引用,该项目作为对客户记录的引用,具有地址。这些都是1:1映射。


当我尝试保存新活动时会出现问题。当我添加一个新的Activity时,我必须分配它的Project属性,我使用这个代码:

  var   projects  =  来自  p   in   unitOfWork.Datamodel.Project 
                其中  p.ProjectID  ==  MasterModel.CurrentProject.ProjectID
             &NBSP ;  选择  p;
item.Project  =  projects.Single< Project >();

然后,当我调用SaveChanges()时,我收到一个UpdateException: 

'Entities.Customer'中的实体参与'FK_Customer_Address'关系。找到0个相关的"地址"。 1'地址'是预期的。


令我感到困惑的是,错误是指在创建活动时我甚至不使用的实体。 活动所需的唯一直接参考应该是项目,因此我提供了一个实例。我没有深度加载
该项目的所有其他关系(例如客户及其所有关系......),因为我认为没有必要,因为在SaveChanges()上,EF应该只有 尝试为一个活动发出INSERT语句,在其Foreign Key属性中传递ProjectID
。至少,这是我的理解。


另一个奇怪的事情是该异常比我加载的项目更深入地了解了一段关系:我没有使用"include"。对客户而言,例外情况是抱怨客户与其子女之间的某种关系。我不明白为什么会这样。因为我使用了UnitOfWork模式,所以我确切知道加载到我的上下文中的内容(使用调试器,我可以看到它)


看起来这个异常告诉我在尝试之前加载更多实体SaveChanges()。 但是,一旦我开始这样做,这是一个没有结束的旅程: 我首先添加了一个包括:

  var   projects  =  来自  p  &NBSP; unitOfWork.Datamodel.Project.Include(QUOT;客户")


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;         where   p.ProjectID  ==  MasterModel.CurrentProject.ProjectID
                select   p;
item.Project  =  projects.Single& lt; 项目>();

但是我在下一级遇到了同样的问题,抱怨地址 对象的属性!


有人能告诉我这里发生了什么以及如何解决这个问题吗?


TIA,

Jeroen




 

解决方案

Hello Jeroen,


 


欢迎来到EF论坛!


 


< span style ="font-family:Calibri">这个问题真的很奇怪。  
我记得以前我看过这种问题,但我不记得根本原因是什么。  
如果方便的话,请给我发一个演示项目和db文件来重现这个问题吗? 
 非常感谢 
我的邮箱:
v- micsun@microsoft.com  


 


顺便说一句,自我跟踪实体未启用延迟加载。
   有关详细信息,请参阅
http://msdn.microsoft.com/en-us/library/ff407090.aspx  


 


美好的一天!


 


< span style ="font-size:12pt">  


最诚挚的问候,b
Lingzhi Sun


MSDN Subscriber Support


如果您对我们的支持有任何反馈,请联系
msdnmg@microsoft.com


Hello, I ran into a problem I do not understeand, I hope someone can help me understand and fix it.

I 'm using C# with Entity Framework 4 and a Self-Tracking entities POCO layer (using the T4 template). My context has LazyLoading = true and ProxyCreationEnabled = false  (but I think I tried all options there)

My database schema is like:
        Activity --> Project --> Customer --> Address   
So an Activity has a reference to a project, which as a reference to a Customer record, which has an address. These are all 1:1 mappings.

The problem occurs when I try to save a new Activity. When I add a new Activity, I have to assign its Project property, I do so using this code:

var projects = from p in unitOfWork.Datamodel.Project
               where p.ProjectID == MasterModel.CurrentProject.ProjectID
               select p;
item.Project = projects.Single<Project>();

Then, when I call SaveChanges() , I get an UpdateException: 
Entities in 'Entities.Customer' participate in the 'FK_Customer_Address' relationship. 0 related 'Address' were found. 1 'Address' is expected.

What puzzles me is that the error refers to entities that I do not even use when creating an activity. The only direct reference required for an activity should be the Project, so I supply it with an instance of that. I do not deep-load all other relations of that project (like the Customer and all its relations...) since I figured it would not be necessary, since on SaveChanges() the EF should only try to issue an INSERT statement for one activity, passing the ProjectID in its Foreign Key property. At least, that's my understanding.

Another strange thing is that the exception compains about a relationship 1 step deeper than the project I loaded: I did not use an "include" on the Customers, yet the exception complains about some relationship between the customer and one of its children. I do not understand why that is. Since I use the UnitOfWork pattern, I know exactly what's loaded into my context (using the Debugger, I can see it

It looks like the exception is telling me to load more entities before attempting the SaveChanges(). But once I start doing so, it's a journey without an end: I first added an include like:

var projects = from p in unitOfWork.Datamodel.Project.Include("Customer")


               where p.ProjectID == MasterModel.CurrentProject.ProjectID
               select p;
item.Project = projects.Single<Project>();

but then I got the same problem at the next level, complaining about properties of the Address object!

Can anyone tell me what's going on here and how I can fix this?

TIA,
Jeroen


 

解决方案

Hello Jeroen,

 

Welcome to EF forum!

 

This issue is really strange.   I remember I saw this kind of problem before, but I cannot recall what the root cause is.   If it is convenient for you, could you please send me a demo project and db file to repro this issue?    Thanks a lot!  My mail: v-micsun@microsoft.com. 

 

BTW, Self-Tracking entities are not enabled to perform lazy loading.   For detail, please see http://msdn.microsoft.com/en-us/library/ff407090.aspx. 

 

Good day!

 

 

Best Regards,
Lingzhi Sun

MSDN Subscriber Support in Forum

If you have any feedback on our support, please contact msdnmg@microsoft.com


这篇关于SaveChanges在未更改的对象上抛出UpdateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆