Data Access has been discontinued. Please refer to this page for more information.

Primitive Key and Reference Type Value

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

This topic demonstrates how to map a dictionary property with primitive key and reference type value. The key here is that you don't need to use any of the extension methods for dictionary configuration. Instead, you have to create a new many-to-many association between the class that contains the dictionary property and the class that represents the dictionary value.

Below is a sample FluentMetadataSource implementation showing you how to configure the DictionaryModel class. The class has a dictionary property with a primitive key and value that is a reference type.

public class FluentModelMetadataSource : FluentMetadataSource
{
   protected override IList<MappingConfiguration> PrepareMapping()
   {
       IList<MappingConfiguration> preparedConfigurations = new List<MappingConfiguration>();

       MappingConfiguration<ValueClass> valueClassConfig = new MappingConfiguration<ValueClass>();
       valueClassConfig.MapType().ToTable( "ValueTable" );
       valueClassConfig.HasProperty( x => x.ValueClassId ).IsIdentity( KeyGenerator.Autoinc );
       preparedConfigurations.Add( valueClassConfig );

       MappingConfiguration<DictionaryModel> dictionaryConfiguration = 
           new MappingConfiguration<DictionaryModel>();
       dictionaryConfiguration.HasProperty( d => d.Id ).ToColumn( "Id" );
       dictionaryConfiguration.HasProperty( d => d.Name ).ToColumn( "Name" );
       dictionaryConfiguration.MapType().ToTable( "DictionaryModel" );
       dictionaryConfiguration.HasProperty( d => d.Id ).IsIdentity( KeyGenerator.Autoinc );

       dictionaryConfiguration.HasAssociation( x => x.IntValueDictionary ).
           MapJoinTable( "JoinTableName", ( x, y, z ) => new
       {
           dictionary = x.Id, // The DictionaryModel.Id property
           key = y,
           value = z.ValueClassId
       } );

       preparedConfigurations.Add( dictionaryConfiguration );
       return preparedConfigurations;
   }
}
Public Class FluentModelMetadataSource
    Inherits FluentMetadataSource
    Protected Overrides Function PrepareMapping() As IList(Of MappingConfiguration)
        Dim preparedConfigurations As IList(Of MappingConfiguration) = _
            New List(Of MappingConfiguration)()

        Dim valueClassConfig As New MappingConfiguration(Of ValueClass)()
        valueClassConfig.MapType().ToTable("ValueTable")
        valueClassConfig.FieldNamingRules.AddPrefix = "_"
        valueClassConfig.HasProperty(Function(x) x.ValueClassId).IsIdentity(KeyGenerator.Autoinc)
        preparedConfigurations.Add(valueClassConfig)

        Dim dictionaryConfiguration As New MappingConfiguration(Of DictionaryModel)()
        dictionaryConfiguration.HasProperty(Function(d) d.Id).ToColumn("Id")
        dictionaryConfiguration.HasProperty(Function(d) d.Name).ToColumn("Name")
        dictionaryConfiguration.FieldNamingRules.AddPrefix = "_"
        dictionaryConfiguration.MapType().ToTable("DictionaryModel")
        dictionaryConfiguration.HasProperty(Function(d) d.Id).IsIdentity(KeyGenerator.Autoinc)

        dictionaryConfiguration.HasAssociation(Of Integer, ValueClass)(Function(x) x.IntValueDictionary).
            MapJoinTable("JoinTableName", Function(x, y, z) New With {Key .dictionary = x.Id,
                Key .key = y,
                Key .value = z.ValueClassId})

        preparedConfigurations.Add(dictionaryConfiguration)
        Return preparedConfigurations
    End Function
End Class

The resulting database schema should look like the snapshot below:

DictionaryModel Class

public class DictionaryModel
{
   public DictionaryModel()
   {
       this.IntValueDictionary = new Dictionary<int, ValueClass>();
   }
   public int Id {get;set;}
   public string Name {get;set;}
   public IDictionary<int, ValueClass> IntValueDictionary {get;set;}
}
Public Class DictionaryModel
    Public Sub New()
        Me.IntValueDictionary = New Dictionary(Of Integer, ValueClass)()
    End Sub
    Private _id As Integer
    Public Property Id() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _intValueDictionary As IDictionary(Of Integer, ValueClass)
    Public Property IntValueDictionary() As IDictionary(Of Integer, ValueClass)
        Get
            Return _intValueDictionary
        End Get
        Set(ByVal value As IDictionary(Of Integer, ValueClass))
            _intValueDictionary = value
        End Set
    End Property
End Class

ValueClass

public class ValueClass
{
   public int ValueClassId {get;set;}
   public string Name {get;set;}
}
Public Class ValueClass
    Private _valueClassId As Integer
    Public Property ValueClassId() As Integer
        Get
            Return _valueClassId
        End Get
        Set(ByVal value As Integer)
            _valueClassId = value
        End Set
    End Property

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
End Class