Entity Framework 4 displays DateTimeOffset date in SQL datetime in Visual Studio 2010

I am using RTM for Visual Studio 2010 with RTM.NET/Entity Framework 4 RTM with a suitable model design. When I create an object with a DateTimeOffset field, the EF model tries to match the DateTimeOffset with SQL dates, not with SQL datetimeoffset. I am using SQL Server 2008 Express, so datetimeoffset is supported in the database.

In Visual Studio, this error occurs:

Error 2019: Membership Indication is invalid. The type "Edm.DateTimeOffset [Nullable = False, DefaultValue =, Precision =]" of the member "Created" in the type "Data.SqlStorage.MyType" is incompatible with the "SqlServer.datetime [Nullable = False, DefaultValue =, Precision = 3] 'member 'Created' in type 'Data.SqlStorage.Store.MyTypes

If I edit the type directly in the xml section of EDMX StorageModels, I get the following error:

Error 40: The datetimeoffset type does not match the namespace or aliases. Only primitive types can be used without qualification.

Why doesn't the fashion designer simply map this to the datetimeoffset SQL type? This issue also occurred when I was still working with beta versions of Visual Studio 2010 and the .NET framework 4.

+4
source share
3 answers

Try a different path (DB-> Model). He worked for Julie Lerman . It seems to me that your manually edited EDMX should also work if you qualify DateTimeOffset with a namespace.

+1
source

In the RTM release, you can do something similar in your DbContext:

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

It is also useful for telling Entity Framework Code. First use datetime2 instead of datetime when creating your database.

+5
source

The solution was to update the DB once, AFTER you manually changed it in sql script and generated db. I did this, and after I checked the table collation, and the data type was changed to DATE instead of DATETIME. I think the same can be applied if you want to switch to DATETIME2.

+1
source

Source: https://habr.com/ru/post/1307027/


All Articles