简体   繁体   English

实体框架alter column type

[英]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. DateTime的默认值为1/1/0001 ,它在SQL Server日期范围之外。

I suppose that you have used CodeFirst to generate the DB. 我想您已经使用CodeFirst生成了数据库。 You currently can't just change the code and automatically alter the DB (this will be possible in EF 5 via Migrations). 当前,您不能只更改代码并自动更改数据库(这在EF 5中可以通过Migrations实现)。 You have to manually alter the DB and set the type of the column to DateTime. 您必须手动更改数据库并将列的类型设置为DateTime。 Or you you delete and recreate the DB from scratch (if you don't need the data that is already stored in it). 或者,您从头删除并重新创建数据库(如果不需要已存储在其中的数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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