Fluent mapping & HasAssociation

2 posts, 0 answers
  1. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 04 Jul 2012 Link to this post

    Hello,

    I'm getting an error (compile) when adding an Association:

    The entities:
    Namespace Entities
     
        Public Class EntityBase
            Implements INotifyPropertyChanged, INotifyPropertyChanging
     
            .....
     
        End Class
     
        Namespace Application
            Namespace Data
     
                Namespace Commerce
                    Public Class ItemGroup
                        Inherits EntityBase
     
                        Public Sub New()
                            Me.Items = New List(Of Item)()
                        End Sub
     
                        Private l_ItemGroupId As Long
                        Public Property ItemGroupId() As Long
                            Get
                                Return l_ItemGroupId
                            End Get
                            Set(ByVal value As Long)
                                l_ItemGroupId = value
                            End Set
                        End Property
     
                        Private l_Name As String
                        Public Property Name() As String
                            Get
                                Return l_Name
                            End Get
                            Set(ByVal value As String)
                                l_Name = value
                            End Set
                        End Property
     
                        Private l_Description As String
                        Public Property Description() As String
                            Get
                                Return l_Description
                            End Get
                            Set(ByVal value As String)
                                l_Description = value
                            End Set
                        End Property
     
                        Private l_Items As IList(Of Item)
                        Public Property Items() As IList(Of Item)
                            Get
                                Return l_Items
                            End Get
                            Set(ByVal value As IList(Of Item))
                                l_Items = value
                            End Set
                        End Property
                    End Class
     
                    Public Class Item
                        Inherits EntityBase
     
                        Private l_ItemId As Long
                        Public Property ItemId() As Long
                            Get
                                Return l_ItemId
                            End Get
                            Set(ByVal value As Long)
                                l_ItemId = value
                            End Set
                        End Property
     
                        Private l_Name As String
                        Public Property Name() As String
                            Get
                                Return l_Name
                            End Get
                            Set(ByVal value As String)
                                l_Name = value
                            End Set
                        End Property
     
                        Private l_Description As String
                        Public Property Description() As String
                            Get
                                Return l_Description
                            End Get
                            Set(ByVal value As String)
                                l_Description = value
                            End Set
                        End Property
     
                        Private l_Discontinued As Boolean?
                        Public Property Discontinued() As Boolean?
                            Get
                                Return l_Discontinued
                            End Get
                            Set(ByVal value As Boolean?)
                                l_Discontinued = value
                            End Set
                        End Property
     
                        Private l_ItemGroup As ItemGroup
                        Public Property ItemGroup() As ItemGroup
                            Get
                                Return l_ItemGroup
                            End Get
                            Set(ByVal value As ItemGroup)
                                l_ItemGroup = value
                            End Set
                        End Property
     
                        Private l_ItemGroupId As Long
                        Public Property ItemGroupId() As Long
                            Get
                                Return l_ItemGroupId
                            End Get
                            Set(ByVal value As Long)
                                l_ItemGroupId = value
                            End Set
                        End Property
     
                    End Class
                End Namespace
            End Namespace
        End Namespace
    End Namespace

    The mapping:
    Protected Overrides Function PrepareMapping() As System.Collections.Generic.IList(Of Telerik.OpenAccess.Metadata.Fluent.MappingConfiguration)
           Dim configurations As List(Of MappingConfiguration) = New List(Of MappingConfiguration)()
     
           Dim itemGroupConfiguration As New MappingConfiguration(Of Entities.Application.Data.Commerce.ItemGroup)()
           itemGroupConfiguration.MapType(Function(p) New With {p}).WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed).ToTable(New TableName("ItemGroups", "Application.Data.Commerce"))
           itemGroupConfiguration.HasProperty(Function(f) f.ItemGroupId).IsIdentity(KeyGenerator.Autoinc).HasFieldName("l_ItemGroupId").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ItemGroupId").IsNotNullable().HasColumnType("bigint").HasPrecision(0).HasScale(0)
           itemGroupConfiguration.HasProperty(Function(f) f.Name).HasFieldName("l_Name").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Name").IsNullable().HasColumnType("nvarchar").HasLength(150)
           itemGroupConfiguration.HasProperty(Function(f) f.Name).HasFieldName("l_Description").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Description").IsNullable().HasColumnType("nvarchar").HasLength(250)
           itemGroupConfiguration.HasProperty(Function(f) f.Items).HasFieldName("l_Items")
           itemGroupConfiguration.FieldNamingRules.AddPrefix = "l_"
           configurations.Add(itemGroupConfiguration)
     
     
           Dim itemConfiguration As New MappingConfiguration(Of Entities.Application.Data.Commerce.Item)()
           itemConfiguration.MapType(Function(p) New With {p}).WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed).ToTable(New TableName("Items", "Application.Data.Commerce"))
           itemConfiguration.HasProperty(Function(f) f.ItemId).IsIdentity(KeyGenerator.Autoinc).HasFieldName("l_ItemId").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ItemId").IsNotNullable().HasColumnType("bigint").HasPrecision(0).HasScale(0)
           itemConfiguration.HasProperty(Function(f) f.Name).HasFieldName("l_Name").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Name").IsNullable().HasColumnType("nvarchar").HasLength(150)
           itemConfiguration.HasProperty(Function(f) f.Name).HasFieldName("l_Description").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Description").IsNullable().HasColumnType("nvarchar").HasLength(250)
           itemConfiguration.HasProperty(Function(f) f.Discontinued).HasFieldName("l_Discontinued").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Discontinued").IsNullable().HasColumnType("bit")
           itemConfiguration.HasProperty(Function(f) f.ItemGroupId).HasFieldName("l_ItemGroupId").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ItemGroupId").IsNotNullable().HasColumnType("bigint").HasPrecision(0).HasScale(0)
           itemConfiguration.HasProperty(Function(f) f.ItemGroup).HasFieldName("l_ItemGroup")
           itemConfiguration.FieldNamingRules.AddPrefix = "l_"
           1>'itemConfiguration.HasAssociation(Function(p) p.ItemGroup).WithOpposite(Function(c) c.Items).HasConstraint(Function(p, c) p.ItemGroupId = c.ItemGroupId)
           2>itemConfiguration.HasAssociation(Of Entities.Application.Data.Commerce.ItemGroup)(Function(p) p.ItemGroup).ToColumn("ItemGroupId")
           configurations.Add(itemConfiguration)


    When commenting 1 & 2, copile is ok and the application works fine, exept the association ofcourse...

    With option 1, there is an error (compile):
    Object reference not set to an instance of an object.
    ExceptionString:
    System.NullReferenceException: Object reference not set to an instance of an object.
       at Telerik.OpenAccess.Metadata.Fluent.FluentMetadataSource.PrepareAssociations()
       at Telerik.OpenAccess.Metadata.Fluent.FluentMetadataSource.CreateModel()
       at Data.FluentDataMetadataSource.CreateModel() in f:\ONTWIKKELING\TEST\FluentMappingDemo\Data\FluentDataMetadataSource.vb:line 62
       at Telerik.OpenAccess.Metadata.Fluent.FluentMetadataSource.GetModelCore(MetadataContainer old)
       at Telerik.OpenAccess.Sdk.Enhancer.Enhancer.CrossDomainRunImpl(AssemblyLoader assemblyLoader)
       at Telerik.OpenAccess.Sdk.Enhancer.EnhancerBase.CrossDomainRun()

    With option 2, there is an error (compile):
    There is no field with name 'l_itemGroup' backing 'ItemGroup' property in type 'Data.Entities.Application.Data.Commerce.Item'. You need to either change the Field Naming rules of the mapping configuration object or call HasFieldName(string) with the name of the backing field.
    ExceptionString:
    System.MissingFieldException: There is no field with name 'l_itemGroup' backing 'ItemGroup' property in type 'Data.Entities.Application.Data.Commerce.Item'. You need to either change the Field Naming rules of the mapping configuration object or call HasFieldName(string) with the name of the backing field.
       at Telerik.OpenAccess.Metadata.Fluent.PropertyInfoHelper.GetFieldInfo(String fieldName, String propertyName, Type type)
       at Telerik.OpenAccess.Metadata.Fluent.PropertyConfiguration.GetMemberType()
       at Telerik.OpenAccess.Metadata.Fluent.PropertyConfiguration.AddConceptualItem(FluentMetadataSource fluentMappingSource, MetaType persistentType)
       at Telerik.OpenAccess.Metadata.Fluent.MappingConfiguration.AddEntityMapping(MetadataContainer metadataContainer, MetaTable table)
       at Telerik.OpenAccess.Metadata.Fluent.MappingConfiguration.AddEntityMapping(MetadataContainer metadataContainer)
       at Telerik.OpenAccess.Metadata.Fluent.FluentMetadataSource.ProcessEntities(MetadataContainer metadataContainer)
       at Telerik.OpenAccess.Metadata.Fluent.FluentMetadataSource.CreateModel()
       at Data.FluentDataMetadataSource.CreateModel() in f:\ONTWIKKELING\TEST\FluentMappingDemo\Data\FluentDataMetadataSource.vb:line 62
       at Telerik.OpenAccess.Metadata.Fluent.FluentMetadataSource.GetModelCore(MetadataContainer old)
       at Telerik.OpenAccess.Sdk.Enhancer.Enhancer.CrossDomainRunImpl(AssemblyLoader assemblyLoader)
       at Telerik.OpenAccess.Sdk.Enhancer.EnhancerBase.CrossDomainRun()

    Well, there IS a field 'l_itemGroup' backing 'ItemGroup' property in type 'Data.Entities.Application.Data.Commerce.Item' ... 

    Can you see something I'm missing?

    Thanks a lot and a great weekend!

    Erik





  2. Erik
    Erik avatar
    314 posts
    Member since:
    Feb 2008

    Posted 04 Jul 2012 Link to this post

    Hello,

    Found the problem... It is a case issue. I changed the NamingRules to use PascalCase and the problem was solved.

    Dim configurations As List(Of MappingConfiguration) = New List(Of MappingConfiguration)()
    Dim DefaultFieldNamingRules As New NamingRules()
    DefaultFieldNamingRules.AddPrefix = "l_"
    DefaultFieldNamingRules.CaseMode = CaseChangeModes.PascalCase
     
    Dim itemGroupConfiguration As New MappingConfiguration(Of Entities.Application.Data.Commerce.ItemGroup)()
    itemGroupConfiguration.MapType(Function(p) New With {p}).WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed).ToTable(New TableName("ItemGroups", "Application.Data.Commerce"))
    itemGroupConfiguration.HasProperty(Function(f) f.ItemGroupId).IsIdentity(KeyGenerator.Autoinc).HasFieldName("l_ItemGroupId").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ItemGroupId").IsNotNullable().HasColumnType("bigint").HasPrecision(0).HasScale(0)
    itemGroupConfiguration.HasProperty(Function(f) f.Name).HasFieldName("l_Name").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Name").IsNullable().HasColumnType("nvarchar").HasLength(150)
    itemGroupConfiguration.HasProperty(Function(f) f.Name).HasFieldName("l_Description").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Description").IsNullable().HasColumnType("nvarchar").HasLength(250)
    'itemGroupConfiguration.HasProperty(Function(f) f.Items).HasFieldName("l_Items")
    itemGroupConfiguration.FieldNamingRules = DefaultFieldNamingRules
    configurations.Add(itemGroupConfiguration)
     
     
    Dim itemConfiguration As New MappingConfiguration(Of Entities.Application.Data.Commerce.Item)()
    itemConfiguration.MapType(Function(p) New With {p}).WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed).ToTable(New TableName("Items", "Application.Data.Commerce"))
    itemConfiguration.HasProperty(Function(f) f.ItemId).IsIdentity(KeyGenerator.Autoinc).HasFieldName("l_ItemId").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ItemId").IsNotNullable().HasColumnType("bigint").HasPrecision(0).HasScale(0)
    itemConfiguration.HasProperty(Function(f) f.Name).HasFieldName("l_Name").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Name").IsNullable().HasColumnType("nvarchar").HasLength(150)
    itemConfiguration.HasProperty(Function(f) f.Name).HasFieldName("l_Description").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Description").IsNullable().HasColumnType("nvarchar").HasLength(250)
    itemConfiguration.HasProperty(Function(f) f.Discontinued).HasFieldName("l_Discontinued").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("Discontinued").IsNullable().HasColumnType("bit")
    itemConfiguration.HasProperty(Function(f) f.ItemGroupId).HasFieldName("l_ItemGroupId").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("ItemGroupId").IsNotNullable().HasColumnType("bigint").HasPrecision(0).HasScale(0)
    itemConfiguration.FieldNamingRules = DefaultFieldNamingRules
    itemConfiguration.HasAssociation(Function(p) p.ItemGroup).WithOpposite(Function(c) c.Items).HasConstraint(Function(p, c) p.ItemGroupId = c.ItemGroupId)
    configurations.Add(itemConfiguration)



    Thanks,

    Erik
  3. DevCraft banner
Back to Top