1

我在 EF Code First 4.3 中针对 SqlCe4.0 客户端创建数据库时遇到循环引用错误。为了清楚起见,我附上了一张我想要的数据库模式的图片。仅供参考,我已经在 SQL Server 2008 中成功创建了我想要的架构。

引用关系将导致不允许的循环引用。[ 约束名称 = FK_Routes_Seasons_SeasonID ]

  • Routes 与 Seasons 有一对多的关系
  • Contracts 与 Routes 有一对多的关系
  • 合同与季节有一对多的关系

所需的数据库架构

我的课程代码是:

Public Class Route
    Public Property RouteID as Integer

    Public Property SeasonID as Integer
    Public Overridable Property Season As Season

    Public Overridable Property Contracts As ICollection(Of Contract)
End Class

Public Class Season
    Public Property SeasonID as Integer

    Public Overridable Property Routes As ICollection(Of Routes)
    Public Overridable Property Contracts As ICollection(Of Contract)
End Class

Public Class Contract
    Public Property ContractID As Integer

    Public Property RouteID As Integer
    Public Overridable Property Route As Route

    Public Property SeasonID As Integer
    Public Overridable Property Season As Season
End Class
4

1 回答 1

2

它不一定是周期性的,而是您在删除树的同一个表中有多个引用。看到这个帖子这个帖子

在你的表中:

赛季 -> 路线 -> 合同

赛季 -> 合同

您需要对其进行设置,以使这些关系之一不会级联删除。我不熟悉 VB,但在 C# 中你可以像这样设置流利:

modelBuilder.Entity<Routes >()
            .HasRequired(r => r.Season )
            .WithMany(s => s.Routes)
            .HasForeignKey(r => r.SeasonID );
            .WillCascadeOnDelete(false);
于 2012-08-23T11:29:27.577 回答