Datetime2 conversion in EF4

2019-04-29 04:22发布

问题:

well, after having spent endless time googling for an answer , i have to add another question in this matter...

i have a table with 2 datetime fields, generated by EF4 EDMX designer in VS 2010.

the generated fields in SQL 2008 are datetime and not datetime2.

looking at the edmx markup, the fields are of type datetime2.

the insert is failing with the known conversion issue datetime2 / datetime....

i have manually changed the fields to datetime2 on the sql table.

still no luck.

i am probably missing something here but can't figure out what the heck is going on.

i even tried regenerating the db from the designer and recreating the edmx from the db...

still the same issue.

any help is greatly apreciated.

回答1:

I had a similar issue once caused by creating the model from SQL server 2005 and then trying to access using SQL server 2008. I fixed this by editing the edmx file using notepad++ or equivalent changing the 2005 to 2008 in the following 2nd line of the edmx file:

<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">

The other thing to try is deleting the table in the edmx and right click Update Model From Database to re-add it hopefully with the right data type this time round.



回答2:

One solution to this problem is to make sure that you correctly initialize the datetime in your entities. Datetime is a struct, it will be initialized to 0 by default.

I did search for that one a little while.