Link Search Menu Expand Document

Map


This attribute maps a class and its properties to the corresponding table and columns in the database.

Class Mapping

Given a table [department].[Person]:

CREATE TABLE [department].[Person]
(
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](256) NOT NULL
)
ON [PRIMARY];
GO

And a Person model class where the default schema does not match:

[Map("[department].[Person]")] // Use the mapping as the default schema is [dbo]
public class Person
{
    public long Id { get; set; }
    public string Name { get; set; }
}

The mapping also applies when the table name and class name differ only in casing.

Property Mapping

Given a table [dbo].[Person]:

CREATE TABLE [dbo].[Person]
(
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [LName] [nvarchar](128) NOT NULL,
    [LName] [nvarchar](128) NOT NULL
)
ON [PRIMARY];
GO

And a Person model class where property names do not match the column names:

public class Person
{
    public long Id { get; set; }
    [Map("FName")]
    public string FirstName { get; set; }
    [Map("LName")]
    public string LastName { get; set; }
}

Retrieval

Use ClassMappedNameCache to retrieve the class mapping.

var mappedName = ClassMappedNameCache.Get<Person>();

For property-level mappings, use either of the following.

var properties = PropertyCache.Get<Person>();

// Iterate the properties
properties
    .AsList()
    .ForEach(property =>
    {
        var mappedName = property.GetMappedName(); // or PropertyMappedNameCache.Get(property.PropertyInfo);
    });