Byte Array Properties
This topic demonstrates how to describe byte array properties in backend independent manner. The Fluent Mapping API exposes the following methods that allow you to map byte array properties in an abstract way:
- WithVariableLength - specifies that the byte[] property should be mapped using variable length. This method will map the byte[] property to a varbinary(N) column.
- WithInfiniteLength - specifies that the byte[] property should be mapped using infinite length. This method will map the byte[] property to a varbinary(max) column.
The following example demonstrates how to map the byte[] properties of the Product class using the Backend Independent API.
public class FluentModelMetadataSource : FluentMetadataSource
{
protected override IList<MappingConfiguration> PrepareMapping()
{
List<MappingConfiguration> configurations = new List<MappingConfiguration>();
MappingConfiguration<Product> productConfiguration = new MappingConfiguration<Product>();
productConfiguration.MapType().ToTable( "Products" );
productConfiguration.HasProperty( x => x.ID ).IsIdentity( KeyGenerator.Autoinc );
productConfiguration.HasProperty( p => p.LargeImage ).WithVariableLength( 250 ).ToColumn( "LargeImage" );
productConfiguration.HasProperty( p => p.SmallImage ).WithInfiniteLength().ToColumn( "SmallImage" );
configurations.Add( productConfiguration );
return configurations;
}
}
Public Class FluentModelMetadataSource
Inherits FluentMetadataSource
Protected Overrides Function PrepareMapping() As IList(Of MappingConfiguration)
Dim configurations As New List(Of MappingConfiguration)()
Dim productConfiguration As New MappingConfiguration(Of Product)()
productConfiguration.MapType().ToTable("Products")
productConfiguration.HasProperty(Function(x) x.ID).IsIdentity(KeyGenerator.Autoinc)
productConfiguration.HasProperty(Function(p) p.LargeImage).WithVariableLength(250).ToColumn("LargeImage")
productConfiguration.HasProperty(Function(p) p.SmallImage).WithInfiniteLength().ToColumn("SmallImage")
productConfiguration.FieldNamingRules.AddPrefix = "_"
configurations.Add(productConfiguration)
Return configurations
End Function
End Class
The result database table will have the following structure:
Product Class
public class Product
{
public int ID
{
get;
set;
}
public byte[] LargeImage
{
get;
set;
}
public byte[] SmallImage
{
get;
set;
}
}