a simple API to map POCO properties to database columns when using Dapper
Dapper.FluentMap provides a simple API to map your POCO properties to database columns when using Dapper. This avoids the need of specifying column aliases in your queries and keeps your POCO's clean of mapping attributes.
To install the NuGet package, enter the folowing in the package manager console:
PM> Install-Package Dapper.FluentMap
Dapper.FluentMap supports manual mapping and convention based mapping.
A manual map would look like:
public class ProductMap : EntityMap<Product>
{
public ProductMap()
{
// Map property 'Name' to column 'strName'.
Map(p => p.Name)
.ToColumn("strName");
// Ignore the 'LastModified' property when mapping.
Map(p => p.LastModified)
.Ignore();
}
}
A typical convention implementation looks like this:
public class TypePrefixConvention : Convention
{
public TypePrefixConvention()
{
// Map all properties of type int and with the name 'id' to column 'autID'.
Properties<int>()
.Where(c => c.Name.ToLower() == "id")
.Configure(c => c.HasColumnName("autID"));
// Prefix all properties of type string with 'str' when mapping to column names.
Properties<string>()
.Configure(c => c.HasPrefix("str"));
// Prefix all properties of type int with 'int' when mapping to column names.
Properties<int>()
.Configure(c => c.HasPrefix("int"));
}
}
For more info about mapping, configuration and initialization: see the docs.