程序包管理器控制台中的Update-Database失败.我使用了Entity Framework 6.x和代码优先方法.错误是
"数据库中已经有一个名为'AboutUs'的对象."
我怎么解决这个问题?
internal sealed class Configuration 
    : DbMigrationsConfiguration<Jahan.Blog.Web.Mvc.Models.JahanBlogDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = false;
    }
    protected override void Seed(Jahan.Blog.Web.Mvc.Models.JahanBlogDbContext context)
    {
    }
}
我的DbContext是:
public class JahanBlogDbContext : IdentityDbContext<User, Role, int, UserLogin, UserRole, UserClaim>
{
    public JahanBlogDbContext()
        : base("name=JahanBlogDbConnectionString")
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<JahanBlogDbContext>());
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Entity<Comment>().HasRequired(t => t.Article).WithMany(t => t.Comments).HasForeignKey(d => d.ArticleId).WillCascadeOnDelete(true);
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<User>().ToTable("User");
        modelBuilder.Entity<Role>().ToTable("Role");
        modelBuilder.Entity<UserRole>().ToTable("UserRole");
        modelBuilder.Entity<UserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<UserClaim>().ToTable("UserClaim");
    }
    public virtual DbSet<Article> Articles { get; set; }
    public …database entity-framework ef-code-first ef-migrations entity-framework-6
在我的MVC应用程序中,我使用了Entity Framework 6并使用代码优先方法创建了数据库.经过一段时间后,我通过添加新列并删除了一些列来更新其中一个实体类.为了将这些更改反映到数据库,我按照以下步骤操作:
- 删除了项目中的迁移文件夹.
- 删除了数据库中的__MigrationHistory表.
然后在程序包管理器控制台中运行以下命令:
Enable-Migrations -EnableAutomaticMigrations -Force
在配置文件中添加以下行:
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
运行:
Add-Migration Initial- 最后,运行:
Update-Database -Verbose
但是,我遇到一个错误"数据库中已经有一个名为'xxx'的对象."
为了摆脱这个问题,我在第5步之后创建的初始文件中注释Up方法中的代码.这可以防止错误,但数据库中没有任何更改(更新的实体表保持不变).哪里出错了?在此先感谢您的帮助.
这是我在migration.cs文件中注释的Up方法:
    public override void Up()
    {
        CreateTable(
            "dbo.City",
            c => new
                {
                    ID = c.Int(nullable: false, identity: true),
                    Name = c.String(nullable: false),
                    RegionID = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.ID)
            .ForeignKey("dbo.Region", t => t.RegionID)
            .Index(t => t.RegionID);
        CreateTable(
            "dbo.Multiplier",
            c => new
                {
                    ID = c.Int(nullable: false, identity: true),
                    Status = c.Int(nullable: false),
                    Term = …sql-server asp.net-mvc entity-framework updatemodel ef-code-first