Using Transient Properties
The purpose of this topic is to show you how to configure transient properties. Transient properties are properties that will not have corresponding database representation. There are two ways in which to configure transient properties:
- You could explicitly mark the property as transient by using the AsTransient method.
- Configure partially mapped models
Explicitly marking a property as transient
The following example demonstrates how to configure the LastName property of the Person class as transient. When you update your database to the latest model state, a column for the LastName property won't be created.
MappingConfiguration<Person> personConfiguraiton = new MappingConfiguration<Person>();
personConfiguraiton.MapType().ToTable( "People" );
personConfiguraiton.HasProperty( p => p.Id ).IsIdentity();
personConfiguraiton.HasProperty( p => p.LastName ).AsTransient();
Dim personConfiguraiton As New MappingConfiguration(Of Person)()
personConfiguraiton.MapType().ToTable("People")
personConfiguraiton.HasProperty(Function(p) p.Id).IsIdentity()
personConfiguraiton.FieldNamingRules.AddPrefix = "_"
personConfiguraiton.HasProperty(Function(p) p.LastName).AsTransient()
Configuring partially mapped models
The second way is to configure partially mapped models. For this approach, you have to use explicit mapping configuration. When you update your database to the latest model state, the LastName and Address columns will be skipped.
MappingConfiguration<Person> personConfiguraiton = new MappingConfiguration<Person>();
personConfiguraiton.MapType( p => new
{
Id = p.Id,
FirstName = p.FirstName
} ).ToTable( "People" );
personConfiguraiton.HasProperty( p => p.Id ).IsIdentity();
Dim personConfiguraiton As New MappingConfiguration(Of Person)()
personConfiguraiton.MapType(Function(p) New With {Key .Id = p.Id,
Key .FirstName = p.FirstName}).ToTable("People")
personConfiguraiton.HasProperty(Function(p) p.Id).IsIdentity()
personConfiguraiton.FieldNamingRules.AddPrefix = "_"
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