4

I'm using Visual Studio 2010 RTM with .NET/Entity Framework 4 RTM with a model driven design approach. When I create an entity with a DateTimeOffset field the EF modeler tries to map the DateTimeOffset to a SQL datetime instead of a SQL datetimeoffset. I'm using SQL Server 2008 Express so the datetimeoffset is supported in the database.

Visual Studio comes up with this error:

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

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

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

Why doesn't the modeler just correctly map this to a SQL datetimeoffset type? This problem also occured when I was still working with the beta versions of Visual Studio 2010 & .NET framework 4.

Carvellis
  • 3,992
  • 2
  • 34
  • 66

3 Answers3

5

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

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

This is also useful for telling Entity Framework Code First to use datetime2 instead of datetime when generating your database.

bkaid
  • 51,465
  • 22
  • 112
  • 128
1

Try going the other way (DB->Model). It worked for Julie Lerman. It seems to me your manually-edited EDMX should also work if you qualify the DateTimeOffset with a namespace.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • 1
    The solution was to update from DB once. After that something probably got fixed, because now I can manually change the type to datetimeoffset without VS complaining. Still a bit annoyed I have to manually change it and the modeler doesn't set it correctly itself. – Carvellis Jun 02 '10 at 14:12
  • You can go DB=>Model to see how it's done but I found out that adding the `precision="7"` attribute to the XML fixes this issue. – Dennis Rongo Dec 03 '14 at 19:18
1

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

denstorti
  • 127
  • 2
  • 12