简体   繁体   中英

Entity Framework alter column type

I have created a column in my database using this property:

public int EnteredDateTime { get; set; }

The column is created correctly, however, when I update the property:

public DateTime EnteredDateTime { get; set; }

I am getting the following error:

The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

What am I doing wrong?

This would happen if you forgot to set the property.

The default value for a DateTime is 1/1/0001 , which is outside the range of SQL Server dates.

I suppose that you have used CodeFirst to generate the DB. You currently can't just change the code and automatically alter the DB (this will be possible in EF 5 via Migrations). You have to manually alter the DB and set the type of the column to DateTime. Or you you delete and recreate the DB from scratch (if you don't need the data that is already stored in it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM