When utilizing entity framework with Visual Studio 2010.net 4.0, you get a 'datetime2' issue.

c# entity-framework sql-server sql-server-2008

Question

Having this issue:

System.Data.SqlClient.SqlException : The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.

All of my entity objects correspond to DB objects.

Google yielded only one instance of this error:

Google outcome

After reading this, I suddenly remembered that we upgraded the entity model from Visual Studio 2010 and added 2 fields. What he means by "hand coding" the discrepancies is unclear to me. I cannot spot any.

Coding-wise, all I'm doing is filling the entity object before saving. I filled the date field with (and used code to fill the new fields):DateTime.Now ..

This is the crucial portion of the code:ctx.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

SQL Server 2008 serves as the database.

Thoughts?

The remaining errors are:

at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at SafariAdmin.Site.WebServices.SpeciesPost.SaveOrUpdateSpecies(String sid, String fieldName, String authToken) in SpeciesPost.svc.cs: line 58 at SafariAdmin.TestHarness.Tests.Site.WebServices.SpeciesPostSVC_Tester.SaveNewSpecies() in SpeciesPostSVC_Tester.cs: line 33 --SqlException at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlDataReader.ConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary2 identifierValues, List1 generatedValues) at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)

1
60
8/2/2017 9:29:23 AM

Accepted Answer

If your database columns are Datetime, this might be an issue since Entity Framework treats all dates as a Datetime2. The most common fixes, according to what we discovered after experiencing the identical issue, are to fill in all the date fields and change the datatype.

65
6/8/2016 3:11:40 PM

Popular Answer

Any optional components must be declared if using Code First.DateTime as a propertyDateTime? or Nullable<DateTime> . UnsetDateTime Issues may arise from items.

If a standard property in the database is nullableDateTime code (not inDateTime? When this occurs, ADO.NET will issue an insert instruction with the date 0001-01-01 (notNULL ), yet there is a problem since the smallest SQL DateTime value is 1753-01-01. When yourDateTime The code has a nullable property, such asDateTime? or Nullable<DateTime> ), the insert command is going to try to insert aNULL rather than an unsuitable date.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow