实体框架代码第一个日期字段创建

sfg*_*ups 77 entity-framework code-first entity-framework-4.1

我正在使用Entity Framework Code First方法来创建我的数据库表.以下代码DATETIME在数据库中创建一个列,但我想创建一个DATE列.

[DataType(DataType.Date)]
[DisplayFormatAttribute(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime ReportDate { get; set; }
Run Code Online (Sandbox Code Playgroud)

如何DATE在创建表时创建类型列?

Lad*_*nka 154

尝试使用ColumnAttributefrom System.ComponentModel.DataAnnotations(在EntityFramework.dll中定义):

[Column(TypeName="Date")]
public DateTime ReportDate { get; set; }
Run Code Online (Sandbox Code Playgroud)

  • 在EF6中,您需要使用System.ComponentModel.DataAnnotations.Schema;` (7认同)
  • 这对我来说就像一个魅力. (2认同)
  • 如果您指向不支持日期的数据库,您可能会收到错误消息,`Sequence contains no matching element`。SQL Server 2014 确实如此。 (2认同)

Ric*_*ard 20

EF6版David Roth的答案如下:

public class DataTypePropertyAttributeConvention 
    : PrimitivePropertyAttributeConfigurationConvention<DataTypeAttribute>
{
    public override void Apply(ConventionPrimitivePropertyConfiguration configuration, 
        DataTypeAttribute attribute)
    {
        if (attribute.DataType == DataType.Date)
        {
            configuration.HasColumnType("Date");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

像以前一样注册:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     base.OnModelCreating(modelBuilder);

     modelBuilder.Conventions.Add(new DataTypePropertyAttributeConvention());
}
Run Code Online (Sandbox Code Playgroud)

这与Tyler Durden的方法具有相同的结果,除了它使用EF基类来完成这项工作.


Tyl*_*den 9

我发现这很适合在EF6中使用.

我创建了一个用于指定数据类型的约定.此约定将数据库创建中的默认DateTime数据类型从datetime更改为datetime2.然后,它将更具体的规则应用于我使用DataType(DataType.Date)属性修饰的任何属性.

public class DateConvention : Convention
{
    public DateConvention()
    {
        this.Properties<DateTime>()
            .Configure(c => c.HasColumnType("datetime2").HasPrecision(3));

        this.Properties<DateTime>()
            .Where(x => x.GetCustomAttributes(false).OfType<DataTypeAttribute>()
            .Any(a => a.DataType == DataType.Date))
            .Configure(c => c.HasColumnType("date"));
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在您的上下文中注册then convention:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    modelBuilder.Conventions.Add(new DateConvention());
    // Additional configuration....
}
Run Code Online (Sandbox Code Playgroud)

将该属性添加到您希望仅为日期的任何DateTime属性:

public class Participant : EntityBase
{
    public int ID { get; set; }

    [Required]
    [Display(Name = "Given Name")]
    public string GivenName { get; set; }

    [Required]
    [Display(Name = "Surname")]
    public string Surname { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Date of Birth")]
    public DateTime DateOfBirth { get; set; }
}
Run Code Online (Sandbox Code Playgroud)


Sto*_*mov 6

如果你不想用属性来装饰你的类,你可以像这样在DbContext's 中设置OnModelCreating它:

public class DatabaseContext: DbContext
{
    // DbSet's

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // magic starts
        modelBuilder.Entity<YourEntity>()
                    .Property(e => e.ReportDate)
                    .HasColumnType("date");
        // magic ends

        // ... other bindings
    }
}
Run Code Online (Sandbox Code Playgroud)


Irf*_*raf 6

我使用以下

    [DataType(DataType.Time)]
    public TimeSpan StartTime { get; set; }

    [DataType(DataType.Time)]
    public TimeSpan EndTime { get; set; }

    [DataType(DataType.Date)]
    [Column(TypeName = "Date")]
    public DateTime StartDate { get; set; }

    [DataType(DataType.Date)]
    [Column(TypeName = "Date")]
    public DateTime EndDate { get; set; }
Run Code Online (Sandbox Code Playgroud)

使用Entity Framework 6和SQL Server Express 2012 - 11.0.2100.60(X64).它完美地工作并在sql server中生成时间/日期列类型