在最开始的时候,我是这样定义模型的:
public class Category
{
public long CategoryId { get; set;}
public string CategoryName { get; set; }
public virtual ICollection<ContentInfo> Contents { get; set; }
}
Public class Article
{
public int ContentId { get; set; }
public string Content { get; set; }
[ForeignKey("Category")]
public long CategoryId { get; set; }
public virtual Category Category { get; set; }
}在使用Automatic-Migration从模型生成数据库之后,我将CategoryId的类型从"long“更改为"int",并再次使用Automatic-Migration更新数据库。
这次抛出了一个异常,告诉我主键和外键引用了"CategoryId“列,所以迁移失败了。如果我手动删除主键和外键,一切都是正常的。但我想让自动迁移为我做这件事,可以吗?
发布于 2012-12-26 12:04:27
我自己还没有尝试过这样的迁移,但我记得我看到过一种选择
公共类MYMigrationConfiguration : DbMigrationsConfiguration {
public MyMigrationConfiguration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true; //have you tried this ?
}否则,您可能需要为这种类型的更改执行基于代码的迁移
发布于 2013-04-22 23:17:48
您可以使用"Sever Explore“手动操作表结构,例如,删除键约束,或删除整个表。并让EF生成全新的表定义。
https://stackoverflow.com/questions/14035813
复制相似问题