The Entity Framework Code first conventions name the database column name after the property names. You can override this behavior using the Column Attribute.
Column Convention
Consider the following model. The Default convention names the database fields after the property name. The property data type determines the column type of the database field. The order of the fields in the table follows the order in which we define properties in the model.
1 2 3 4 5 6 7 | public class Employee { public int EmployeeID { get; set; } public string Name { get; set; } } |
The following table shows how data types are mapped to the database columns.
Data Type | Mapped to |
---|---|
string | nvarchar(max) |
decimal | decimal(18, 2), not null |
decimal? | decimal(18, 2), null |
double | float, not null |
double? | float, null |
int | int, not null |
int? | int, null |
bool | bit, not null |
bool? | bit, null |
DateTime | datetime, not null |
DateTime ? | datetime, null |
byte[] | varbinary(max) |
Column Attribute
By Applying the Column
attribute, we can change the column name, datatype, and order of the column. The attribute takes the following argument
1 2 3 | [Column (string name, Properties:[Order = int],[TypeName = string]) |
Where
Name: Name of the database column
Order: Sets the zero-based Order of the field in the table
TypeName: Database Provider-specific data type of the column the property
Column Name
Column
attributes used to specify the column names. EF by convention creates the columns using the property name. If the Column
attribute is present, then the EF will use the attribute value as the column name when creating the table
1 2 3 4 5 6 7 8 | public class Employee { public int EmployeeID { get; set; } [Column("EmployeeName")] public string Name { get; set; } } |
Data type
Use the TypeName
option to set the data type of the column. In the following example, EmployeeName
is set as the varchar
data type. Name1
is set as the nvarchar
data type. TextColumn
as the ntext
data type and amount
as money
data type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Employee { public int EmployeeID { get; set; } [Column("EmployeeName", TypeName = "varchar")] public string Name { get; set; } [Column(TypeName = "nvarchar")] public string Name1 { get; set; } [Column(TypeName = "varchar")] public string Address { get; set; } [Column(TypeName = "text")] public string TextColumn { get; set; } [Column(TypeName = "money")] public decimal Amount { get; set; } [Column(TypeName = "decimal")] public decimal Rate { get; set; } } |
TypeName
attribute is different from the DataType
data annotation attribute, which is used only for the UI Validations
The Entity Framework does not allow us to set the field length in TypeName attribute
Column Order
Specify the order of the column using the Order
option. The Order can be any integer value. The EF will create the column based on the Order specified. The Unordered column appears after the ordered column as shown in the image below
1 2 3 4 5 6 7 8 9 10 11 | public class Employee { public int EmployeeID { get; set; } public string Name { get; set; } [Column(Order = 4, TypeName = "varchar")] public string Column1 { get; set; } [Column(Order = 3, TypeName = "Varchar")] public string Column2 { get; set; } } |
Thanks providing such a nice information
Hello Sir,
Please provide me details about what the is column attribute.
thank you for providing the information
Thank you for providing the step wise information about data annotation column attibute