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

Primitive Key-Value Types

This topic demonstrates how to map a dictionary property with primitive key and value. Generally, you can choose from or combine the following main options:

  • You can specify the name of the column to which the dictionary key will be mapped using the WithKeyColumn method.
  • You can specify the Telerik Data Access type of the dictionary key column using the WithKeyOpenAccessType method.
  • You can specify the name of the column to which the dictionary value will be mapped using the WithValueColumn method.
  • You can specify the Telerik Data Access type of the dictionary value column using the WithValueOpenAccessType method.
  • You can specify the length of the dictionary value column using the HasValueLength method.
  • You can specify the name of the foreign key column for the dictionary table using the WithForeignKey method.
  • You can specify the name of the table to which the dictionary property will be mapped using the WithTable method.

To use the extension methods for configuring of dictionary properties you need to use/import the Telerik.OpenAccess.Metadata.Fluent.Advanced namespace.

For example, the DictionaryModel class contains a dictionary property named IntStringDictionary. The key and the value of that dictionary are primitive types. The following code snippet is a sample FluentMetadataSource implementation illustrating how to configure the DictionaryModel class mapping:

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

        MappingConfiguration<DictionaryModel> dictionaryModelMappingConfig = 
            new MappingConfiguration<DictionaryModel>();

        dictionaryModelMappingConfig.MapType().ToTable("DictionaryModel");

        dictionaryModelMappingConfig.HasProperty(dictClass => dictClass.Id).
            IsIdentity(KeyGenerator.Autoinc).ToColumn("DictionaryModelId");
        dictionaryModelMappingConfig.HasProperty(dictClass => dictClass.Name).ToColumn("Name");

        dictionaryModelMappingConfig.HasProperty(dictClass => dictClass.IntStringDictionary)
            .WithKeyColumn("DictionaryKey")
            .WithKeyOpenAccessType(OpenAccessType.Int32)
            .WithValueColumn("DictionaryValue")
            .WithValueOpenAccessType(OpenAccessType.StringFixedLength)
            .HasValueLength(128)
            .WithForeignKey("DictionaryFK")
            .WithTable("DictionaryTable");

        mappingConfigurations.Add(dictionaryModelMappingConfig);

        return mappingConfigurations;
    }
}
Partial Public Class FluentModelMetadataSource
    Inherits FluentMetadataSource

    Protected Overrides Function PrepareMapping() As IList(Of MappingConfiguration)
        Dim mappingConfigurations As New List(Of MappingConfiguration)()

        Dim dictionaryModelMappingConfig As New MappingConfiguration(Of DictionaryModel)()

        dictionaryModelMappingConfig.MapType().ToTable("DictionaryModel")

        dictionaryModelMappingConfig.FieldNamingRules.AddPrefix = "_"
        dictionaryModelMappingConfig.HasProperty(Function(dictClass) dictClass.Id). _
            IsIdentity(KeyGenerator.Autoinc).ToColumn("DictionaryModelId")
        dictionaryModelMappingConfig.HasProperty(Function(dictClass) dictClass.Name). _
            ToColumn("Name")

        dictionaryModelMappingConfig.HasProperty(Of Integer, String)(Function(dictClass) dictClass.IntStringDictionary).
            WithKeyColumn("DictionaryKey").
            WithKeyOpenAccessType(OpenAccessType.Int32).
            WithValueColumn("DictionaryValue").
            WithValueOpenAccessType(OpenAccessType.StringFixedLength).
            HasValueLength(128).
            WithForeignKey("DictionaryFK").
            WithTable("DictionaryTable")

        mappingConfigurations.Add(dictionaryModelMappingConfig)

        Return mappingConfigurations
    End Function    
End Class

The resulting database schema should look like the one on the screenshot below:

DictionaryModel Class

class DictionaryModel
{
    public DictionaryModel()
    {
        this.IntStringDictionary = new Dictionary<int, string>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public IDictionary<int, string> IntStringDictionary { get; set; }
}
Public Class DictionaryModel
    Private _id As Integer
    Private _name As String
    Private _intStringDictionary As IDictionary(Of Integer, String)

    Public Sub DictionaryModel()
        Me.IntStringDictionary = New Dictionary(Of Integer, String)
    End Sub

    Public Property Id As Integer
        Get
            Return Me._id
        End Get

        Set(value As Integer)
            Me._id = value
        End Set
    End Property

    Public Property Name As String
        Get
            Return Me._name
        End Get

        Set(value As String)
            Me._name = value
        End Set
    End Property

    Public Property IntStringDictionary As IDictionary(Of Integer, String)
        Get
            Return Me._intStringDictionary
        End Get

        Set(value As IDictionary(Of Integer, String))
            Me._intStringDictionary = value
        End Set
    End Property
End Class