Entity Framework Code First Date field creation

后端 未结 8 2282
情歌与酒
情歌与酒 2020-11-30 23:13

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

8条回答
  •  死守一世寂寞
    2020-11-30 23:58

    If you prefer not to decorate your classes with attributes, you can set this up in the DbContext's OnModelCreating like this:

    public class DatabaseContext: DbContext
    {
        // DbSet's
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            // magic starts
            modelBuilder.Entity()
                        .Property(e => e.ReportDate)
                        .HasColumnType("date");
            // magic ends
    
            // ... other bindings
        }
    }
    

提交回复
热议问题