I am using Entity Framework Code First method to create my database table. The following code
creates a DATETIME column in the database, but I want to create a
Beside using ColumnAttribute you can also create a custom attribute convention for the DataTypeAttribute:
public class DataTypePropertyAttributeConvention : AttributeConfigurationConvention
{
public override void Apply(PropertyInfo memberInfo, PrimitivePropertyConfiguration configuration, DataTypeAttribute attribute)
{
if (attribute.DataType == DataType.Date)
{
configuration.ColumnType = "Date";
}
}
}
Just register the convention in your OnModelCreating method:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Add(new DataTypePropertyAttributeConvention());
}