Link Search Menu Expand Document

Class Mapping


This feature allows you to map .NET CLR types or class properties to their equivalent database objects and types. It covers class and property name mapping, primary and identity columns, class and property handlers, property attributes, and database types.

Class Name Mapping

Use the Map attribute to map the class name.

[Map("[sales].[Customer]")]
public class Customer
{
    ...
}

Or use the Table attribute from the System.ComponentModel.DataAnnotations.Schema namespace.

[Table("[sales].[Customer]")]
public class Customer
{
    ...
}

Or use the FluentMapper class for an attribute-free setup. It uses ClassMapper underneath.

FluentMapper
    .Entity<Customer>()
    .Table("[sales].[Customer]")

Property Name Mapping

Use the Map attribute to map a property name.

public class Customer
{
    public int Id { get; set; }

    [Map("FName")]
    public string FirstName { get; set;}

    [Map("LName")]
    public string LastName { get; set;}

    ...
}

Or use the Column attribute from the System.ComponentModel.DataAnnotations.Schema namespace.

public class Customer
{
    public int Id { get; set; }

    [Column("FName")]
    public string FirstName { get; set;}

    [Column("LName")]
    public string LastName { get; set;}

    ...
}

Or use the FluentMapper class for an attribute-free setup. It uses PropertyMapper underneath.

FluentMapper
    .Entity<Customer>()
    .Column(e => e.FirstName, "[FName]")
    .Column(e => e.LastName, "[LName]");

Primary Mapping

Use the Primary attribute to designate a primary property.

public class Customer
{
    [Primary]
    public int Id { get; set; }

    ...
}

Or use the Key attribute from the System.ComponentModel.DataAnnotations namespace.

public class Customer
{
    [Key]
    public int Id { get; set; }

    ...
}

Or use the FluentMapper class for an attribute-free setup. It uses PrimaryMapper underneath.

FluentMapper
    .Entity<Customer>()
    .Primary(e => e.Id);

Identity Mapping

Use the Identity attribute to designate an identity property.

public class Customer
{
    [Identity]
    public int Id { get; set; }

    ...
}

Or use the FluentMapper class for an attribute-free setup. It uses IdentityMapper underneath.

FluentMapper
    .Entity<Customer>()
    .Identity(e => e.Id);

Class Handler Mapping

Use the ClassHandler attribute to map a class handler.

[ClassHandler(CustomerClassHandler)]
public class Customer
{
    public int Id { get; set; }
    ...
}

Or use the FluentMapper class for an attribute-free setup. It uses ClassHandlerMapper underneath.

FluentMapper
    .Entity<Customer>()
    .ClassHandler<CustomerClassHandler>();

Property Handler Mapping

Use the PropertyHandler attribute to map a property handler to a class property.

public class Customer
{
    public int Id { get; set; }

    ...

    [PropertyHandler(PersonAddressPropertyHandler)]
    public Address Address { get; set; }

    ...
}

Or use the FluentMapper class for an attribute-free setup. It uses PropertyHandlerMapper underneath.

FluentMapper
    .Entity<Customer>()
    .PropertyHandler<PersonAddressPropertyHandler>(e => e.Address);

Database Type Mapping

Use the TypeMap attribute to map a class property to a database type.

public class Customer
{
    public int Id { get; set; }

    ...

    [TypeMap(DbType.DateTime2)]
    public DateTime DateOfBirth { get; set; }

    ...
}

Or use the FluentMapper class for an attribute-free setup. It uses TypeMapper underneath.

FluentMapper
    .Entity<Customer>()
    .Column(e => e.DateOfBirth, DbType.DateTime2);

Type Level

Use FluentMapper or TypeMapper to map a .NET CLR type to its equivalent database type.

FluentMapper
    .Type<DateTime>()
    .DbType(DbType.DateTime2);

Please visit the Type Mapping feature for further information.