c# - 强制 Entity Framework 5 使用 datetime2 数据类型

标签 c# entity-framework ef-code-first entity-framework-5

在使用 Code-First 模型时,是否可以全局设置 Entity Framework DbContext 以将 datetime2 用于所有 System.DateTime 的属性?

我可以使用 HasColumnType() 方法为每一列执行此操作,但对于现有代码库,我想要一个全局解决方案。

最佳答案

由于 EF6 已经推出一段时间了,这个问题仍然出现在搜索中,这里是使用自定义约定来设置 SQL 类型的方法。在 DbContext 类的 OnModelCreating 方法中执行:

modelBuilder.Properties<DateTime>()
    .Configure(c => c.HasColumnType("datetime2"));

关于c# - 强制 Entity Framework 5 使用 datetime2 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15248488/

相关文章:

c# - Signalr .net 客户端

c# - Azure负载测试: Spin up multiple instances runtime

c# - 使用参数创建自定义异常

Twitter API 的 C# DateTime created_at 字段

c# - EntityFramework 正在删除我在创建 dbcontext 时的记录

c# - 如何更新 Entity Framework 中的相关实体

sql-server - Azure SQL 频繁连接超时

asp.net-mvc-3 - 数据注释首先使用代码在主键上设置身份种子值

c# - 使用 Linq to Entities 进行分组和多重排序

c# - 访问/使用 View Entity Framework 6 Code First 方法