EF4 - 强制执行顺序
我有三个表——上下文、会话和用户。下面是它们的简化结构。
用户:
用户 ID (Int, PK)
电子邮件 (Varchar(100))
上下文:
ContextId (Int, PK)
UserId(Int,来自用户表的 FK)
CurrentSessionId(Int、Nullable、FK,来自会话表)
会话:
SessionId(Int,PK)
UserId(Int,来自用户表的 FK)
ContextId (Int, FK from Context table)
下面是我的代码中删除用户实体的序列。
1) 将 Context 表的 CurrentSessionId 更新为 null,其中 UserId = 要删除的 UserId。
2)删除该User对应的所有Session
3)删除该User对应的所有Context
4)最后删除用户。
5) 调用 ObjectContext.SaveChanges()
调用 ObjectContext.SaveChanges() 时出现以下异常 -
无法确定相关操作的有效顺序。由于外键约束、模型要求或存储生成的值,可能会存在依赖关系。
我猜 EF4 在确定前 3 个语句的执行顺序时会感到困惑。 是否可以告诉 EF4 首先执行哪些语句以及接下来执行哪些语句?
我可以解决的一种方法是在第一步之后调用 SaveChanges(),然后在第四步之后再次调用它。但我很想知道是否还有其他优雅的解决方案。
I have three tables - Context, Session and User. And below are their simplified structure.
User:
UserId (Int, PK)
Email (Varchar(100))
Context:
ContextId (Int, PK)
UserId (Int, FK from User table)
CurrentSessionId (Int, Nullable, FK from Session table)
Session:
SessionId (Int, PK)
UserId (Int, FK from User table)
ContextId (Int, FK from Context table)
Below is the sequence in my code to delete a user entity.
1) Update CurrentSessionId of Context table to null where UserId = UserId to be deleted.
2) Delete all Sessions corresponding to the User
3) Delete all Contexts corresponding to the User
4) Finally Delete User.
5) Call ObjectContext.SaveChanges()
Upon calling ObjectContext.SaveChanges() I get the following exception -
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
I guess EF4 gets confused determining the execution order of first 3 statements. Is it possible to tell EF4 what statements to execute first and what to execute next?
One way I can work around is to call SaveChanges() after step one and then call it again after step four. But I would love to know if there are any other elegant solution than that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于此 MSDN 链接 EF4 中不能强制执行顺序。我必须在第一步之后调用 SaveChanges() 两次,然后在第四步之后再次调用 SaveChanges() 两次。
Based on this MSDN link the order of execution cannot be forced in EF4. I had to call SaveChanges() twice once after step one and then again after step four.
我认为问题在于
Session
和Context
之间的循环引用;如果您删除Context.CurrentSessionId
那么您生成的Context
对象仍将具有您可以使用的Sessions
属性,但 EF 不应获得试图协商依赖关系时感到沮丧。您可以像这样手动将CurrentSession
属性添加到Context
中:此外,如果您设置
User ->上下文
和用户-> EF 模型中的会话
关系以级联删除(或者更好 在数据库中),那么删除一个User
就变成了两行:I think the issue lies with the circular reference between
Session
andContext
; if you get rid ofContext.CurrentSessionId
then your generatedContext
object will still have aSessions
property you can use, but EF shouldn't get upset trying to negotiate the dependencies. You could manually add aCurrentSession
property toContext
like this:Also, if you set the
User -> Context
andUser -> Session
relationships in your EF model to cascade delete (or even better in the database), then deleting aUser
becomes just two lines: