none
Entity configuration of optional children RRS feed

  • Question

  • I'm trying to map to a DB that I cannot alter. Everything seemed to work until I started to work on adding entities. I think I have a simple scenario where child records (AssignmentMapping) may or may not exist. In the DB the children are created via a trigger after the parent (ClientMapping) which has an identity for the key.  So my parent is something like this:

    Public Class ClientMapping
        Implements IValidatableObject
    #Region "properties"
        Public Property ClientMappingID() As Int32
        Public Property AppUserID() As String
        Public Property ClientID() As Int32
        Public Property Allocation() As Int32
        Public Property UpdatedByAppUserID() As String
        Public Property UpdateDate() As DateTime?
        Public Overridable Property Parent() As MappingRecord
        Public Overridable Property Client() As Client
        Public Overridable Property AssignmentMapping() As AssignmentMapping
    #End Region

    And the optional child is like this:

    Public Class AssignmentMapping
        Implements IValidatableObject
    #Region "public properties"
        Public Property AssignmentMappingID() As Int32
        Public Property ClientMappingID() As Int32
        Public Property IsNative() As Boolean
        Public Property AppUserID() As String
        Public Property ClientID() As Int32?
        Public Property Allocation() As Int32
        Public Property StartDate() As DateTime?
        Public Property EndDate() As DateTime?
        Public Property IsActive() As Boolean
        Public Property UpdateDate() As DateTime
        Public Property LastReviewDate() As DateTime?
        Public Property UpdatedByAppUserID() As String
        Public Overridable Property Parent() As MappingRecord
        Public Overridable Property ClientMapping() As ClientMapping
        Public Overridable Property Client() As Client
    #End Region

    For the configuration I have this for the parent:

    Imports System.Data.Entity.ModelConfiguration
    Public Class ClientMappingConfiguration
        Inherits EntityTypeConfiguration(Of ClientMapping)
        Public Sub New()
            'map to existing table
            ToTable("tbl_ClientMapping")
            HasKey(Function(k) k.ClientMappingID)
            HasRequired(Of MappingRecord)(Function(fk) fk.Parent).WithMany(Function(emp) emp.Clients).HasForeignKey(Function(cm) cm.AppUserID)
            HasRequired(Of Client)(Function(fk) fk.Client)
            HasOptional(Of AssignmentMapping)(Function(nav) nav.AssignmentMapping).WithRequired()
            'map columns to entity
            [Property](Function(r) r.ClientMappingID).HasColumnName("cmId").HasColumnOrder(1).HasDatabaseGeneratedOption(ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)
        End Sub
    End Class

    I think the problem is in the child configuration. As you can see in the comments, either I have an issue when I try to add data or when I try to read from the DB.

    Imports System.Data.Entity.ModelConfiguration
    Public Class AssignmentMappingConfiguration
        Inherits EntityTypeConfiguration(Of AssignmentMapping)
        Public Sub New()
            'map to existing table
            ToTable("t_AssignmentDates")
            HasKey(Function(k) k.AssignmentMappingID)
            HasRequired(Of MappingRecord)(Function(fk) fk.Parent).WithMany(Function(emp) emp.Assignments).HasForeignKey(Function(cm) cm.AppUserID)
            'this works for reading but when clientmapping was added it threw error "a dependent property in a referential constraint is mapped to a store-generated column (cmId)
            HasOptional(Function(x) x.ClientMapping).WithRequired()
            'add works but fails on read "The navigation property of type 'rp_ef_cf_dal.AssignmentMapping' is not a single implementation of 'System.Collections.Generic.ICollection`1[T]' "
            'HasRequired(Of ClientMapping)(Function(fk) fk.ClientMapping).WithMany(Function(cm) cm.AssignmentMapping).HasForeignKey(Function(fk) fk.ClientMappingID)
        End Sub
    End Class

    Like the code says, either I get "a dependent property in a referential constraint is mapped to a store-generated column" or I get " The navigation property of type 'rp_ef_cf_dal.AssignmentMapping' is not a single implementation of 'System.Collections.Generic.ICollection`1[T]".
    Tuesday, June 5, 2012 9:59 PM

Answers

  • This might be the solution. Appears to eliminate the errors.

    HasRequired(Of ClientMapping)(Function(fk) fk.ClientMapping).WithOptional(Function(cm) cm.AssignmentMapping)

    • Marked as answer by pretzelb Wednesday, June 6, 2012 1:17 PM
    Wednesday, June 6, 2012 12:32 AM

All replies

  • This might be the solution. Appears to eliminate the errors.

    HasRequired(Of ClientMapping)(Function(fk) fk.ClientMapping).WithOptional(Function(cm) cm.AssignmentMapping)

    • Marked as answer by pretzelb Wednesday, June 6, 2012 1:17 PM
    Wednesday, June 6, 2012 12:32 AM
  • Hi pretzelb,

    Welcome to MSDN Forum.

    Yes, the modified code is correct. For your statement, the relationship between 'ClientMapping' and 'AssignmentMapping' is 1 to 0..1, but in your initial post, the relationship is 0..1 to 1, so the exception was thrown, please use the modified code instead.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, June 6, 2012 2:54 AM
    Moderator