2010-04-15 84 views
4

我使用Visual Studio 2010 RTM与.NET/Entity Framework 4 RTM一起使用模型驱动的设计方法。 当我使用DateTimeOffset字段创建实体时,EF建模者试图将DateTimeOffset映射到SQL日期时间而不是SQL datetimeoffset。 我正在使用SQL Server 2008 Express,因此datetimeoffset在数据库中受支持。实体框架4将DateTimeOffset映射到Visual Studio 2010中的SQL datetime

的Visual Studio想出了这个错误:

Error 2019: Member Mapping specified is not valid. The type 'Edm.DateTimeOffset[Nullable=False,DefaultValue=,Precision=]' of member 'Created' in type 'Data.SqlStorage.MyType' is not compatible with 'SqlServer.datetime[Nullable=False,DefaultValue=,Precision=3]' of member 'Created' in type 'Data.SqlStorage.Store.MyTypes

如果我直接在EDMX StorageModels XML节我收到以下错误编辑类型:

Error 40: The Type datetimeoffset is not qualified with a namespace or alias. Only PrimitiveTypes can be used without qualification.

为什么没有建模只是正确地将其映射到SQL datetimeoffset类型? 当我还在使用Visual Studio 2010的测试版本时,也出现了此问题。& .NET Framework 4.

回答

1

尝试以其他方式(DB-> Model)。 It worked for Julie Lerman。在我看来,如果您使用命名空间限定DateTimeOffset,则您的手动编辑的EDMX也应该可以工作。

+1

解决方案是从DB更新一次。之后,可能会得到解决,因为现在我可以手动将类型更改为datetimeoffset而无需抱怨VS。仍然有点恼火,我不得不手动改变它,而建模者本身并没有正确设置它。 – Carvellis 2010-06-02 14:12:15

+0

你可以去DB => Model来看看它是如何完成的,但是我发现在XML中增加'precision =“7”'属性可以解决这个问题。 – 2014-12-03 19:18:14

1

解决方案是从DB更新一次,之后在脚本中手动更改sql并生成数据库。我做了它,并检查了我的表映射后,数据类型被修改为DATE而不是DATETIME。我认为如果您想更改为DATETIME2,可以应用相同的方法。

5

在RTM版本中,你可以做这样的事情在你的DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder) { 
    modelBuilder.Entity<EntityName>().Property(p => p.PropertyName).HasColumnType("datetimeoffset"); 
} 

这也告诉了实体框架代码首先要产生你的数据库,而不是使用日期时间的DATETIME2有用。