我添加了
public IEnumerable<Comment> Comments { get; set; }
到ASP.net MVC项目中的模型。我运行以下命令在程序包控制台中生成迁移
PM> Add-Migration AddCommentsToDevice
并且生成的迁移没有获得对模型的更改
public partial class AddCommentsToDevice : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
有关迁移语法或导致检测的原因的任何提示?
答案 0 :(得分:3)
你加入了一点点。您需要正确配置关系 - 最好用流畅的api。用于导航属性:
public virtual ICollection<Comment> Comments { get; set; }
重要 - 始终使用ICollection,而不是IEnumerable导航属性并使其成为虚拟 - 由于此ef将能够跟踪更改。
然后在您的DbContext中添加以下代码:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Model>().HasKey(x => x.Id);
modelBuilder.Entity<Model>().HasMany(x => x.Comments);
base.OnModelCreating(modelBuilder);
}
我假设与评论相关的类名为Model。