Entity Framework 4.1 代码优先 KeyAttribute 作为非身份列
我的代码优先模型有问题。数据现在位于数据库中,因此我无法使用 DropCreateDatabaseIfModelChanges 类重新设定数据库种子,但我需要更改一个表,以便 bigint 列不是 IDENTITY(1,1)。我已经设法使用 SSMS 做到这一点,但现在我的 EF 代码说它已经过时了。 这是相关表的代码:
public class Vote {
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public long FacebookUserId { get; set; }
[Required]
public Entity Entity { get; set; }
}
所以我更改了我的表架构和模型(我认为这是它的反映,但我显然错了),但 EF 仍然说我的模型已超出日期,我无法重新播种数据库以使其“完美”。
任何帮助将不胜感激。
谢谢,
本杰明
I've got a problem with a code-first model I've got. Data is now in the database so I can't re-seed the database using a DropCreateDatabaseIfModelChanges class, but I need to change one table so that a bigint column is not an IDENTITY(1,1). I've managed to do this using SSMS but now my EF code is saying it's out of date.
This is the code for the table in question:
public class Vote {
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public long FacebookUserId { get; set; }
[Required]
public Entity Entity { get; set; }
}
So I've changed my table schema, and my model (which I thought was the reflection of it, but I'm obviously wrong), but EF is still saying my model is out of date, and I can't re-seed the database to get it "perfect".
Any help would be much appreciated.
Thanks,
Benjamin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将其添加到您的 OnModelCreating 中:
这应该会消除模型已过期的异常,但在此之前您必须始终手动同步模型和数据库。
Try add this to your
OnModelCreating
:That should remove the exception that model is out of date but till this time you must always synchronize model and database manually.
使用数据注释:
使用 Fluent API:
Using data annotation:
Using fluent API:
参考这篇文章 ...
似乎实体框架默认情况下期望您插入到标识列中。
要解决这个问题,请尝试
或用以下内容装饰 POCO 中的密钥...
referring to this post ...
It seems that entity framework expects by default that you insert into identity column.
to solve this try
or decorate your key in the POCO with ...