Link Search Menu Expand Document

Class Mapping


This is a feature that would allow you to map any .NET CLR types or class properties into its equivalent database objects and types. It includes the mapping capabilities for the class/property name, primary/identity columns, class/property handlers, property attributes and the database types.

Class Name Mapping

To map the class name, simply use the Map attribute.

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

Or, use the Table attribute of System.ComponentModel.DataAnnotations.Schema namespace.

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

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

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

Property Name Mapping

To map the property name, simply use the Map attribute.

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 the Column attribute of 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 attribute-free setup. It uses the PropertyMapper underneath.

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

Primary Mapping

To map the class primary property, simply use the Primary attribute.

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

    ...
}

Or, use the Key attribute of System.ComponentModel.DataAnnotations namespace.

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

    ...
}

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

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

Identity Mapping

To map the class identity property, simply use the Identity attribute.

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

    ...
}

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

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

Class Handler Mapping

To map the class handler, simply use the ClassHandler attribute.

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

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

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

Property Handler Mapping

To map the class property equivalent property handler, simply use the PropertyHandler attribute.

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

    ...

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

    ...
}

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

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

Database Type Mapping

To map the class property equivalent database type, simply use the TypeMap attribute.

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

    ...

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

    ...
}

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

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

Type Level

You can also use the FluentMapper or the TypeMapper classes to map the .NET CLR type into its equivalent database type.

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

Please visit the Type Mapping feature for further information.