The Column Attribute

The Column attribute is applied to a property to specify the database column that the property should map to when the entity's property name and the database column name differ. The following example maps the Title property in the Book entity to a database column named Description:

public class Book
{
    public int BookId { get; set; }
    [Column("Description")]
    public string Title { get; set; }
    public Author Author { get; set; }
}

You can also use the Column attribute to specify the ordinal position (Order property) of the column in the resulting table, and its database-specific data type (TypeName property):

public class Book
{
    public int BookId { get; set; }
    [Column("Description", Order = 2, TypeName = "nvarchar(100)")]
    public string Title { get; set; }
    public Author Author { get; set; }
}

Fluent API

The Fluent API equivalent to the Column attribute is HasColumnName. The Fluent API equivalent to specifying the data type is HasColumnType. Column ordering is not implemented in Fluent API.


Created:
Last updated: 14/03/2017 16:33:43
Proficiency Level: Beginner