Mixing Default and Explicit Mapping
Telerik Data Access Fluent Mapping API gives you an option to mix default mapping with explicit mapping. That means that you can map part of your model by using explicit mapping and use default mapping for the rest of the model. The following example demonstrates how to configure the Person class by using default and explicit mapping. The key point here is the usage of the UseDefaultMap method.
public class FluentModelMetadataSource : FluentMetadataSource
{
protected override IList<MappingConfiguration> PrepareMapping()
{
List<MappingConfiguration> configurations = new List<MappingConfiguration>();
MappingConfiguration<Person> personConfiguraiton = new MappingConfiguration<Person>();
personConfiguraiton.MapType( p => new
{
Id = p.Id,
LastName = p.LastName
} ).UseDefaultMap().ToTable( "People" );
personConfiguraiton.HasProperty( p => p.Id ).IsIdentity( KeyGenerator.Autoinc );
configurations.Add( personConfiguraiton );
return configurations;
}
}
Public Class FluentModelMetadataSource
Inherits FluentMetadataSource
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 personConfiguraiton As New MappingConfiguration(Of Person)()
personConfiguraiton.MapType(Function(p) _
New With {Key .Id = p.Id, Key .LastName = p.LastName}). _
UseDefaultMap().ToTable("People")
personConfiguraiton.HasProperty(Function(p) p.Id).IsIdentity()
personConfiguraiton.FieldNamingRules.AddPrefix = "_"
configurations.Add(personConfiguraiton)
Return configurations
End Function
End Class
When you update your database to the latest model state, the result table will have the following structure:
Fluent Mapping API allows you to map a class to more than one table in the database. Read more
Person Class
public class Person
{
public int Id {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string Address {get;set;}
}
Public Class Person
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 _firstName As String
Public Property FirstName() As String
Get
Return _firstName
End Get
Set(ByVal value As String)
_firstName = value
End Set
End Property
Private _lastName As String
Public Property LastName() As String
Get
Return _lastName
End Get
Set(ByVal value As String)
_lastName = value
End Set
End Property
Private _address As String
Public Property Address() As String
Get
Return _address
End Get
Set(ByVal value As String)
_address = value
End Set
End Property
End Class