【发布时间】:2014-05-16 13:36:38
【问题描述】:
我使用 EF6 和 MVC5 我在我的项目中使用代码优先。这是我的 DbContext:
public class ProjectServiceDBContext:DbContext
{
public ProjectServiceIranDBContext()
: base("ProjectServiceDBContext")
{ }
public DbSet<Service> Services { get; set; }
public DbSet<FormPost> FormPosts { get; set; }
public DbSet<Factory> Factories { get; set; }
public DbSet<PictureGallery> PictureGallery { get; set; }
public DbSet<Blog> Blog { get; set; }
public DbSet<Comment> Comment { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ServiceMapping());
modelBuilder.Configurations.Add(new FactoryMapping());
modelBuilder.Configurations.Add(new FormPostMapping());
modelBuilder.Configurations.Add(new PictureGalleryMapping());
modelBuilder.Configurations.Add(new BlogMapping());
modelBuilder.Configurations.Add(new CommentMapping());
modelBuilder.Configurations.Add(new UserMapping());
}
}
在我将新字段添加到我的博客模型之前,一切都很好。 我使用迁移来更新我的数据库。但它仅适用于一次调试,如果我停止调试并再次开始调试,我会收到更新数据库的错误:
支持“ProjectServiceDBContext”上下文的模型在创建数据库后发生了变化。考虑使用 Code First 迁移来更新数据库 (http://go.microsoft.com/fwlink/?LinkId=238269)。
每次调试前我都应该使用 add-migration 和 update-database
我该怎么办?
我还在我的解决方案中删除了 Migration 文件夹,并删除了我在数据库中的表,然后重试,但它不起作用。我也创建了新的 DbContext 但仍然存在此错误。
【问题讨论】:
标签: c# entity-framework ef-code-first asp.net-mvc-5