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

Reference Key-Value Types

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 key and value that are reference types.

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

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

       MappingConfiguration<KeyClass> keyClassConfig = new MappingConfiguration<KeyClass>();
       keyClassConfig.MapType().ToTable( "KeyTable" );
       keyClassConfig.HasProperty( x => x.KeyClassId ).IsIdentity( KeyGenerator.Autoinc );
       preparedConfigurations.Add( keyClassConfig );

       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.KeyValueDictionary ).
            MapJoinTable( "JoinTableName", ( x, y, z ) => new
       {
           x.Id, // The DictionaryModel.Id property
           y.KeyClassId,
           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 keyClassConfig As New MappingConfiguration(Of KeyClass)()
        keyClassConfig.MapType().ToTable("KeyTable")
        keyClassConfig.FieldNamingRules.AddPrefix = "_"
        keyClassConfig.HasProperty(Function(x) x.KeyClassId).IsIdentity(KeyGenerator.Autoinc)
        preparedConfigurations.Add(keyClassConfig)

        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.MapType().ToTable("DictionaryModel")
        dictionaryConfiguration.FieldNamingRules.AddPrefix = "_"
        dictionaryConfiguration.HasProperty(Function(d) d.Id).IsIdentity(KeyGenerator.Autoinc)

        dictionaryConfiguration.HasAssociation(Of KeyClass, ValueClass)(Function(x) x.KeyValueDictionary).
            MapJoinTable("JoinTableName", Function(x, y, z) New With {Key x.Id,
                Key y.KeyClassId,
                Key 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.KeyValueDictionary = new Dictionary<KeyClass, ValueClass>();
   }
   public int Id {get;set;}
   public string Name {get;set;}
   public IDictionary<KeyClass, ValueClass> KeyValueDictionary {get;set;}
}
Public Class DictionaryModel
    Public Sub New()
        Me.KeyValueDictionary = New Dictionary(Of KeyClass, 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 _keyValueDictionary As IDictionary(Of KeyClass, ValueClass)
    Public Property KeyValueDictionary() As IDictionary(Of KeyClass, ValueClass)
        Get
            Return _keyValueDictionary
        End Get
        Set(ByVal value As IDictionary(Of KeyClass, ValueClass))
            _keyValueDictionary = value
        End Set
    End Property
End Class

KeyClass

public class KeyClass
{
   public int KeyClassId {get;set;}
   public string Name {get;set;}
}
Public Class KeyClass
    Private _keyClassId As Integer
    Public Property KeyClassId() As Integer
        Get
            Return _keyClassId
        End Get
        Set(ByVal value As Integer)
            _keyClassId = 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

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