none
EdmAssociationConstraint For Composite Keys RRS feed

  • Question

  • "System.Data.Entity.Edm.EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical."

    Public Class ObjContext
        Inherits Global.System.Data.Entity.DbContext
    
        Public Property Obj1Set As Global.System.Data.Entity.DbSet(Of Obj1)
        Public Property Obj2Set As Global.System.Data.Entity.DbSet(Of Obj2)
        Public Property Obj3Set As Global.System.Data.Entity.DbSet(Of Obj3)
    End Class
    
    <Global.System.ComponentModel.DataAnnotations.Table("Obj1")> _
    Public Class Obj1
        <Global.System.ComponentModel.DataAnnotations.Column("Obj1Id", Order:=0),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj1Id As Byte
    
        <Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1Id,Obj2Id")> _
        Public Overridable Property Obj2s As Global.System.Collections.Generic.ICollection(Of Obj2)
    End Class
    
    <Global.System.ComponentModel.DataAnnotations.Table("Obj2")> _
    Public Class Obj2
        Public Property Obj1P As Obj1
        <Global.System.ComponentModel.DataAnnotations.Column("Obj1Id", Order:=0),
        Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1P"),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj1Id As Byte
    
        <Global.System.ComponentModel.DataAnnotations.Column("Obj2Id", Order:=1),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj2Id As Byte
    
        <Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1Id,Obj2Id,Obj3Id")> _
        Public Overridable Property Obj3s As Global.System.Collections.Generic.ICollection(Of Obj3)
    End Class
    
    <Global.System.ComponentModel.DataAnnotations.Table("Obj3")> _
    Public Class Obj3
        Public Property Obj1P As Obj1
        <Global.System.ComponentModel.DataAnnotations.Column("Obj1Id", Order:=0),
        Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1P"),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj1Id As Byte
    
        Public Property Obj2P As Obj2
        <Global.System.ComponentModel.DataAnnotations.Column("Obj2Id", Order:=1),
        Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj2P"),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj2Id As Byte
    
        <Global.System.ComponentModel.DataAnnotations.Column("Obj3Id", Order:=2),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj3Id As Byte
    End Class
    

    Using objC As New ObjContext()
      Dim ob1 As New Obj1()
      objC.Obj1Set.Add(ob1)
    End Using
    

    The issue is involving the keys with Obj3. Obj1 and Obj2 don't seem to have issues when I comment out any reference to Obj3:
    "' Public Property Obj3Set As Global.System.Data.Entity.DbSet(Of Obj3)"
    or
    "Public Overridable Property Obj3s As Global.System.Collections.Generic.ICollection(Of Obj3)"
    Each object's key or composite keys becomes the foreign key for each child object.

    Saturday, January 28, 2012 9:14 PM

Answers

  • Got it.
    Public Class ObjContext
        Inherits Global.System.Data.Entity.DbContext
    
        Public Property Obj1Set As Global.System.Data.Entity.DbSet(Of Obj1)
        Public Property Obj2Set As Global.System.Data.Entity.DbSet(Of Obj2)
        Public Property Obj3Set As Global.System.Data.Entity.DbSet(Of Obj3)
    
    End Class
    
    <Global.System.ComponentModel.DataAnnotations.Table("Obj1")> _
    Public Class Obj1
        <Global.System.ComponentModel.DataAnnotations.Column("Obj1Id", Order:=0),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj1Id As Int64
    
        <Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1Id")> _
        Public Overridable Property Obj2s As Global.System.Collections.Generic.ICollection(Of Obj2)
    End Class
    
    <Global.System.ComponentModel.DataAnnotations.Table("Obj2")> _
    Public Class Obj2
        Public Property Obj1P As Obj1
        <Global.System.ComponentModel.DataAnnotations.Column("Obj1Id", Order:=0),
        Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1P"),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj1Id As Int64
    
        <Global.System.ComponentModel.DataAnnotations.Column("Obj2Id", Order:=1),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj2Id As Int32
    
        <Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1Id,Obj2Id")> _
        Public Overridable Property Obj3s As Global.System.Collections.Generic.ICollection(Of Obj3)
    End Class
    
    <Global.System.ComponentModel.DataAnnotations.Table("Obj3")> _
    Public Class Obj3
        <Global.System.ComponentModel.DataAnnotations.Column("Obj1Id", Order:=0),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj1Id As Int64
    
        <Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1Id,Obj2Id")> _
        Public Property Obj2P As Obj2
        <Global.System.ComponentModel.DataAnnotations.Column("Obj2Id", Order:=1),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj2Id As Int32
    
        <Global.System.ComponentModel.DataAnnotations.Column("Obj3Id", Order:=2),
         Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj3Id As Byte
    End Class

    Using ObjCtx As New ObjContext()
      Dim ob1 As New Obj1()
      ObjCtx.Obj1Set.Add(ob1)
    End Using
    

    • Edited by Nathan-Vindicator Sunday, January 29, 2012 1:29 AM Replaced part solution with complete working solution
    • Marked as answer by Alan_chenModerator Tuesday, January 31, 2012 5:58 AM
    Saturday, January 28, 2012 10:55 PM

All replies

  • Got it.
    Public Class ObjContext
        Inherits Global.System.Data.Entity.DbContext
    
        Public Property Obj1Set As Global.System.Data.Entity.DbSet(Of Obj1)
        Public Property Obj2Set As Global.System.Data.Entity.DbSet(Of Obj2)
        Public Property Obj3Set As Global.System.Data.Entity.DbSet(Of Obj3)
    
    End Class
    
    <Global.System.ComponentModel.DataAnnotations.Table("Obj1")> _
    Public Class Obj1
        <Global.System.ComponentModel.DataAnnotations.Column("Obj1Id", Order:=0),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj1Id As Int64
    
        <Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1Id")> _
        Public Overridable Property Obj2s As Global.System.Collections.Generic.ICollection(Of Obj2)
    End Class
    
    <Global.System.ComponentModel.DataAnnotations.Table("Obj2")> _
    Public Class Obj2
        Public Property Obj1P As Obj1
        <Global.System.ComponentModel.DataAnnotations.Column("Obj1Id", Order:=0),
        Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1P"),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj1Id As Int64
    
        <Global.System.ComponentModel.DataAnnotations.Column("Obj2Id", Order:=1),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj2Id As Int32
    
        <Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1Id,Obj2Id")> _
        Public Overridable Property Obj3s As Global.System.Collections.Generic.ICollection(Of Obj3)
    End Class
    
    <Global.System.ComponentModel.DataAnnotations.Table("Obj3")> _
    Public Class Obj3
        <Global.System.ComponentModel.DataAnnotations.Column("Obj1Id", Order:=0),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj1Id As Int64
    
        <Global.System.ComponentModel.DataAnnotations.ForeignKey("Obj1Id,Obj2Id")> _
        Public Property Obj2P As Obj2
        <Global.System.ComponentModel.DataAnnotations.Column("Obj2Id", Order:=1),
        Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj2Id As Int32
    
        <Global.System.ComponentModel.DataAnnotations.Column("Obj3Id", Order:=2),
         Global.System.ComponentModel.DataAnnotations.Key()> _
        Public Property Obj3Id As Byte
    End Class

    Using ObjCtx As New ObjContext()
      Dim ob1 As New Obj1()
      ObjCtx.Obj1Set.Add(ob1)
    End Using
    

    • Edited by Nathan-Vindicator Sunday, January 29, 2012 1:29 AM Replaced part solution with complete working solution
    • Marked as answer by Alan_chenModerator Tuesday, January 31, 2012 5:58 AM
    Saturday, January 28, 2012 10:55 PM
  • Hi Nathan,

    Thanks for sharing your experience here!

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 31, 2012 5:59 AM
    Moderator