如何在 Entity Framework 4.1 中不使用 CASCADE 创建多对多关系?

发布于 2024-12-09 08:37:09 字数 2619 浏览 0 评论 0原文

我正在使用 Entity Framework 4.1 制作 MVC3 Web 应用程序。问题是我有一个循环引用并且出现异常。我明白为什么这是错误的,所以我想从我的多对多关系中删除 ON DELETE/UPDATE CASCADE 。问题是我不知道该怎么做...... 这就是我的问题的描述。这是我的模型:

问题 ->-。
      |           |
      |           |
    /|\           |
  字段         |
      |           |
      |           |
    /|\           |
 答案 -----'

每个问题都有多个字段,每个字段都有多个答案。当我删除问题或答案时,我想删除相应的答案。我认为这里不需要做额外的工作。但是,每个答案可以有多个相关问题(每个问题都可以依赖于多个答案)。仅当其他问题的答案为“是”时,此关系才用于显示问题,例如...... 现在,如果我删除一个答案,我不想删除依赖于它的所有问题,只删除依赖项。

在我的模型中 Question 对象具有:

' VB
Public Overridable Property AnswerDependencies As List(Of QuestionAnswer)  

// C#
public virtual List < QuestionAnswer > AnswerDependencies { get; set; }  

并且 QuestionAnswer 对象具有:

' VB  
Public Overridable Property DependentQuestions As List(Of Question)  

// C#  
public virtual List < Question > DependentQuestions { get; set; }  

如何在具有以下属性的 DataContext 类中的 OnModelCreating 事件中定义预期行为:

Public Property Questions As DbSet(Of Question)  
Public Property QuestionFields As DbSet(Of QuestionField)  
Public Property QuestionAnswers As DbSet(Of QuestionAnswer)  

编辑

我找到了问题的解决方案!我手动创建了 QuestionAnswerDependency 对象,如下所示:

Public Class QuestionAnswerDependency
    Inherits ModelBase

    Public Property QuestionId As Integer?
    Public Overridable Property Question As Question

    Public Property AnswerId As Integer
    Public Overridable Property Answer As QuestionAnswer

    Sub New(ByRef q As Question, ByRef a As QuestionAnswer)
        Me.New()
        Me.Question = q
        Me.Answer = a
    End Sub

    Sub New()

    End Sub

End Class

我将 QuestionId 属性设置为可为空,这会导致删除时不会出现 CASCADE 并解析循环引用。现在 Question 和 QuestionAnswer 对象有一个 QuestionAnswerDependency 对象列表:

Public Overridable Property QuestionAnswerDependencies As List(Of QuestionAnswerDependency)

我认为 EF 无法自动执行我想要的操作,但如果您知道如何在不创建“中间对象”的情况下实现这一目标,请告诉我。

I am making an MVC3 web app using Entity Framework 4.1. The problem is that I have a cyclical reference and I get an exception. I understand why it is wrong so I would like to remove ON DELETE/UPDATE CASCADE from my many-to-many relationship. The problem is that I do not know how to do that...
That would be it for the description of my problem. Here goes my model:

Questions ->-.
       |              |
       |              |
      /|\             |
  Fields          |
       |              |
       |              |
      /|\             |
 Answers -----'

Every question has multiple fields and every field has multiple answers. When I remove a question or an answer I want to remove the corresponding answers. I think no additional work needs to be done here. However, each answer can have multiple dependent questions (every question can be dependent on many answers). This relationship is used for displaying a question only if an answer to some other question was YES for example...
Now, If I remove an answer I do not want to remove all questions that are dependent on it, only remove the dependency.

In my model Question object has:

' VB
Public Overridable Property AnswerDependencies As List(Of QuestionAnswer)  

// C#
public virtual List < QuestionAnswer > AnswerDependencies { get; set; }  

and QuestionAnswer object has:

' VB  
Public Overridable Property DependentQuestions As List(Of Question)  

// C#  
public virtual List < Question > DependentQuestions { get; set; }  

How to define the anticipated behaviour in OnModelCreating event in my DataContext class that has the following properties:

Public Property Questions As DbSet(Of Question)  
Public Property QuestionFields As DbSet(Of QuestionField)  
Public Property QuestionAnswers As DbSet(Of QuestionAnswer)  

EDIT

I found a solution to my problem! I manually created QuestionAnswerDependency object, that looks like this:

Public Class QuestionAnswerDependency
    Inherits ModelBase

    Public Property QuestionId As Integer?
    Public Overridable Property Question As Question

    Public Property AnswerId As Integer
    Public Overridable Property Answer As QuestionAnswer

    Sub New(ByRef q As Question, ByRef a As QuestionAnswer)
        Me.New()
        Me.Question = q
        Me.Answer = a
    End Sub

    Sub New()

    End Sub

End Class

I made QuestionId property nullable which results in no CASCADE ON DELETE and resolves cyclical reference. Now Question and QuestionAnswer objects have a list of QuestionAnswerDependency objects:

Public Overridable Property QuestionAnswerDependencies As List(Of QuestionAnswerDependency)

I think EF cannot automatically do what I want, but let me know if you have an idea on how to achieve that without creating "an in-between-object".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文