修复使用数据注释在模型生成期间检测到一个或多个验证错误

时间:2013-02-19 13:40:00

标签: .net sql ef-code-first

我使用Sql 2008 r2和visual studio 2010和EF 4.4。并且我得到这个错误运行代码更长时间。守则应解释数据库关系。

  

在模型生成期间检测到一个或多个验证错误:   \ tSystem.Data.Entity.Edm.EdmAssociationConstraint ::关系约束中> Dependent和Principal Roles中的属性数必须相同。

我想使用dataannotation来解决这个问题。我在做什么呢?

“优惠

    Public Class Offer

        <Key(), DatabaseGenerated(DatabaseGeneratedOption.None)>
        Public Property Offer_ID As Integer
        Public Property Name As String

    End Class

“头

Public Class Head

    <Key(), Column(Order:=0), DatabaseGenerated(DatabaseGeneratedOption.None)>
    Public Property Head_ID As Integer

    <ForeignKey("Offer_ID")>
    Public Property Offer As Offer

    <Key(), Column(Order:=1)>
    Public Property Offer_ID As Integer

    Public Property Name As String


End Class

“行

   Public Class Line

        <Key(), Column(Order:=0), DatabaseGenerated(DatabaseGeneratedOption.None)>
        Public Property Line_ID As Integer

        <ForeignKey("Head_ID")>
        Public Property Head As Head

        <Key(), Column(Order:=1)>
        Public Property Head_ID As Integer

        <ForeignKey("Offer_ID")>
        Public Property Offer As Offer

        <Key(), Column(Order:=2)>
        Public Property Offer_ID As Integer

        Public Property Name As String

   End Class

“的DbContext

    Public Class DatabaseContext
        Inherits DbContext

        Public Sub New(p_ConnectionString As String)
            MyBase.New(p_ConnectionString)
        End Sub

        Public Property Offers As DbSet(Of Offer)
        Public Property Heads As DbSet(Of Head)
        Public Property Lines As DbSet(Of Line)

    End Class

'创建一个简单的例子

    Private Shared Sub CreateME()

        Dim offer As New Offer
        offer.Name = "Offer1"
        offer.Offer_ID = 1

        Dim head As New Head
        head.Head_ID = 1
        head.Name = "head1"
        head.Offer = offer
        head.Offer_ID = offer.Offer_ID

        Dim line As New Line
        line.Head = head
        line.Head_ID = head.Head_ID
        line.Line_Id = 1
        line.Name = "line1"
        line.Offer = offer
        line.Offer_ID = offer.Offer_ID

        Using context = New DatabaseContext(GetConnectionString())

            context.Offers.Add(offer)
            context.Heads.Add(head)
            context.Lines.Add(line)
            context.SaveChanges()
        End Using

    End Sub

所以问题是我可以使用数据注释来解决这个问题吗?

我是否必须使用此处说明的模型构建器: How to fix: The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical?

2 个答案:

答案 0 :(得分:2)

首先,我认为您的数据库关系是这样的,我是否正确?

Database Relations

  • Offers有一个简单的主键Offer_ID
  • Heads的复合主键为Head_IDOffer_ID
  • Heads拥有Offers
  • 的外键
  • Lines的复合主键为Line_IDHead_IDOffer_ID
  • Lines拥有Heads
  • 的外键

假设我在阅读你的问题时是对的......


您的代码几近完美。唯一的问题是Line类。在此您指定:

<ForeignKey("Head_ID")>
Public Property Head As Head

但你需要:

<ForeignKey("Head_ID, Offer_ID")>
Public Property Head As Head

一直到一个棘手的逗号分隔字符串!

原因是Heads上的主键是复合键,因此您必须指定所有列才能使关系正确。

答案 1 :(得分:1)

您的Head类具有包含Head_Id和Offer Id的外键。您的Line类需要通过由Head Id和Offer Id组成的外键引用Head。

您可以使用数据注释和Column Order语法。您应该能够在MSDN上找到确切的语法。