Getting this error:
System.Data.SqlClient.SqlException : The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range v
Use that SQL script to convert all the columns from datetime to datetime2
. It skips all the tables contains 'aspnet' for your convenience.
DECLARE @SQL AS NVARCHAR(1024)
DECLARE @TBL AS NVARCHAR(255)
DECLARE @COL AS NVARCHAR(255)
DECLARE @NUL AS BIT
DECLARE CUR CURSOR FAST_FORWARD FOR
SELECT SCHEMA_NAME(t.schema_id)+'.'+t.name, c.name, c.is_nullable
FROM sys.tables AS t
JOIN sys.columns c ON t.object_id = c.object_id
JOIN information_schema.columns i ON i.TABLE_NAME = t.name
AND i.COLUMN_NAME = c.name
WHERE i.data_type = 'datetime' and t.name not like '%aspnet%'
ORDER BY t.name, c.name
OPEN CUR
FETCH NEXT FROM CUR INTO @TBL, @COL, @NUL
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @SQL = 'ALTER TABLE ' + @TBL
+ ' ALTER COLUMN [' + @COL + '] datetime2'
+ (CASE WHEN @NUL=1 THEN '' ELSE ' NOT' END) + ' NULL;'
EXEC sp_executesql @SQL
FETCH NEXT FROM CUR INTO @TBL, @COL, @NUL
END
CLOSE CUR;
DEALLOCATE CUR;
It works for me!
Another possible solution is to set the sql column type of the field to datetime2. this can be done using fluentapi.
Property(x => x.TheDateTimeField)
.HasColumnType("datetime2");
Note: This is a solution for sql server 2008 upwards as datetime2 is not available for sql server 2005 or below.
Simple. On your code first, set the type of DateTime to DateTime?. So you can work with nullable DateTime type in database.
I know that it's an old question, but as I googled here, someone else could do the same ;-)
For ones that changing from DateTime to DateTime2 isn't an option (as for SQL2005 users), I think that in most cases is more reasonable to populate fields left empty with something like
(DateTime)System.Data.SqlTypes.SqlDateTime.MinValue
and not with DateTime.now, as it's easier to recognize it as a "pseudo-null" value (and if it's needed convert it to a real null in a partial class of father object)
This follows on from stepanZ answer... I got this error when using Entity Framework Code First
with AutoMapper
.
When setting up the AutoMapping
we have createddt
, updateddt
, createdby
and updatedby
fields which are automatically set in our public override int SaveChanges()
function. When doing this you need to ensure you set these fields to be ignored by AutoMapper
, otherwise the database will get updated with null
for those fields when they are not supplied from the View
.
My issue was that I had put the source and destination around the wrong way, therefore trying to ignore the fields when setting the ViewModel
, instead of when setting the Model
.
The Mapping
looked like this when I recieved this error (note: the cfg.CreateMap<Source, Destination>()
on the second line is mapping the Model
to the ViewModel
and setting the Ignore()
)
cfg.CreateMap<EventViewModel, Event>();
cfg.CreateMap<Event, EventViewModel>()
.ForMember(dest => dest.CreatedBy, opt => opt.Ignore())
.ForMember(dest => dest.CreatedDt, opt => opt.Ignore())
.ForMember(dest => dest.UpdatedBy, opt => opt.Ignore())
.ForMember(dest => dest.UpdatedDt, opt => opt.Ignore());
The source and destination should be ignored for a mapping from ViewModel
To Model
(note: The code below is correct where the Ignore()
is placed against the mapping for the ViewModel
to the Model
)
cfg.CreateMap<Event, EventViewModel>();
cfg.CreateMap<EventViewModel, Event>()
.ForMember(dest => dest.CreatedBy, opt => opt.Ignore())
.ForMember(dest => dest.CreatedDt, opt => opt.Ignore())
.ForMember(dest => dest.UpdatedBy, opt => opt.Ignore())
.ForMember(dest => dest.UpdatedDt, opt => opt.Ignore());
We had the same issue. This was related to the mssql version. We made it works on all of our version with this method.
Open your edmx file with an xml editor Find this line on the top of your file
<Schema Namespace="XXXXX.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008"
Replace the 2008 by 2005 Save your file, recompile the project.
Hope that will help someone else in the futur.
I only tried this solution with a dbfirst approche.