Entity Framework Datetime2 Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn entity framework datetime2 to datetime error more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags error converting datetime2 to datetime entity framework Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, datetime2 entity framework code first helping each other. Join them; it only takes a minute: Sign up How to fix the datetime2 out-of-range conversion error using DbContext and SetInitializer? up vote 61 down vote favorite 8 I'm using the DbContext and Code First
Conversion From Datetime2 To Datetime Entity Framework
APIs introduced with Entity Framework 4.1. The data model uses basic data types such as string and DateTime. The only data annotation I'm using in some cases is [Required], but that's not on any of the DateTime properties. Example: public virtual DateTime Start { get; set; } The DbContext subclass is also simple and looks like: public class EventsContext : DbContext { public DbSet
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more cannot convert datetime2 to datetime in entity framework about Stack Overflow the company Business Learn more about hiring developers or posting ads
System.data.sqlclient.sqlexception: Error Converting Data Type Datetime2 To Datetime.
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow
Entity Framework Nullable Datetime
is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up conversion of a datetime2 data type to a datetime data type error http://stackoverflow.com/questions/6050906/how-to-fix-the-datetime2-out-of-range-conversion-error-using-dbcontext-and-setin with EF Code first? up vote 4 down vote favorite 2 I'm using EF Code first with my asp.net mvc application. here is my code: Request.RequestDate = DateTime.Now; the type of RequestDate is datetime in my database. and this is error that occurred when i use the above code!: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. please help me. thanx. http://stackoverflow.com/questions/10585214/conversion-of-a-datetime2-data-type-to-a-datetime-data-type-error-with-ef-code-f c# sql-server-2008 entity-framework ef-code-first entity-framework-4.3 share|improve this question asked May 14 '12 at 14:21 Mohammad Zare 54831137 possible duplicate of c# conversion of a datetime2 data type to a datetime data type –CodeCaster May 14 '12 at 14:26 Are you using SQL 2005 ? datetime2 range is from 0001 / 01 / 01 through to 9999 / 12 / 31 hence the error. If so, you'll need to edit the XML edmx file –Jason Jong May 14 '12 at 14:26 @json jong: I'm using sql server 2008 and i have no .edmx file because i use Code first!. –Mohammad Zare May 14 '12 at 14:31 add a comment| 2 Answers 2 active oldest votes up vote 8 down vote accepted Edit: How to fix the datetime2 out-of-range conversion error using DbContext and SetInitializer? The issue is that you are trying to save a value that cannot fit in a SQL datetime column. the answer givin here will stop the conversion from failing. or in your Database change the columns to be type datetime2. I don't exactly know why they code first is generating datetime columns instead of datetime2 Here is an example to explicitly map your Datetime columns to
a datetime2 data type to a datetime data type resulted in an out-of-range value 7 Comments 09 April 2014 13:45 4.43 (7 votes) Entity Framework I managed to generate this error twice within a few days while working with the Entity Framework and SQL http://www.mikesdotnetting.com/article/229/conversion-of-a-datetime2-data-type-to-a-datetime-data-type-resulted-in-an-out-of-range-value Server 2012. I had to resort to searching for a resolution on both occasions so I'm posting about it to sear the cause and solution into my brain. First, note the exception type. It is a SqlException, so it's being http://blog.jongallant.com/2013/07/datetime2-datetime-out-of-range-ef.html generated by the SqlClient provider. In other words, this is your database talking. The SQL Server datetime datatype is capable of storing dates in the range 1753-01-01 to 9999-12-31, or Jan 1st 1753 to way longer than anyone reading entity framework this needs to worry about. The datetime2 data type was introduced in SQL Server 2008. The range of dates that it is capable of storing is 0001-01-01 to 9999-12-31, or Jan 1st 1 AD to way longer than anyone reading this.... You get the idea. The .Net datetime is isomorphic with the SQL Server datetime2 type - the range of possible values is the same. The default value for an uninitialised datetime variable is DateTime.MinValue, or 0001-01-01. And this datetime2 to datetime is usually the cause of the error message featured in the title of this post. It happens when you do not provide a value for a datetime in your C# (or VB) code which is to be stored in a non-nullable datetime field in the database. The .Net variable or property will default to 0001-01-01 which SQL Server will see as a valid value for a datetime2 field, but then tries to convert to a datetime type to store it in the appropriate field in the database table. As has already been established, 0001-01-01 is outside of the range of acceptable values for a datetime field. Hence the error message. So what are the possible fixes? Actually, there are a few: 1. Change the storage to DateTime2 in the database. Datetime2 is the recommended type for dates and times in SQL Server 2008 onwards. You will need to explictly map the relevant columns to datetime2 since EF will always map .Net DateTimes to the SQL Server datetime type. You can do this via the fluent API: modelBuilder.Entity
file, select the field that is causing the error and set StoreGeneratedPattern to Computed. Add the DatabaseGenerated attribute if you are using EF Code First:[DatabaseGenerated(DatabaseGeneratedOption.Computed)]public Nullable