Reference Type Key and Primitive 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 reference type key and primitive 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 key.
Below is a sample FluentMetadataSource implementation showing you how to configure the DictionaryModel class. The class has a dictionary property with a key that is a reference type and primitive value.
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<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.KeyIntDictionary ).
MapJoinTable( "JoinTableName", ( x, y, z ) => new
{
dictionary = x.Id, // The DictionaryModel.Id property
key = y.KeyClassId,
value = z
} );
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 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 KeyClass, Integer)(Function(x) x.KeyIntDictionary).
MapJoinTable("JoinTableName", Function(x, y, z) New With {Key .dictionary = x.Id,
Key .key = y.KeyClassId,
Key .value = z})
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.KeyIntDictionary = new Dictionary<KeyClass, int>();
}
public int Id {get;set;}
public string Name {get;set;}
public IDictionary<KeyClass, int> KeyIntDictionary {get;set;}
}
Public Class DictionaryModel
Public Sub New()
Me.KeyIntDictionary = New Dictionary(Of KeyClass, Integer)()
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 _keyIntDictionary As IDictionary(Of KeyClass, Integer)
Public Property KeyIntDictionary() As IDictionary(Of KeyClass, Integer)
Get
Return _keyIntDictionary
End Get
Set(ByVal value As IDictionary(Of KeyClass, Integer))
_keyIntDictionary = 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