为什么代码首次迁移没有数据库中存在的更新表? [英] Why code first migration no update table that exist in database?

查看:118
本文介绍了为什么代码首次迁移没有数据库中存在的更新表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过代码第一次迁移来更新数据库。例如,我在上下文中有3个实体,在数据库中有1个表,当我运行程序并出现此错误时出现问题:已有表。





迁移配置

I want Update database by code first migration .for example I have 3 entity in contex and 1 table in database and there is a problem when I run program and get this error:There is already table.


Migration Configuration

 internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
       // AutomaticMigrationDataLossAllowed = false;
    }

    protected override void Seed(ApplicationDbContext context)
    {
        //  This method will be called after migrating to the latest version.


    }
}







IdentityModel






IdentityModel

public ApplicationDbContext() : base("DefaultConnection" )
    {
        Database.SetInitializer<applicationdbcontext>(new MigrateDatabaseToLatestVersion<applicationdbcontext,>());
    }

What I have tried:

I have try update database that is already but I can do it.</applicationdbcontext>

推荐答案

更改AutomaticMigrationsEnabled = false; to AutomaticMigrationsEnabled = true;



然后在Package Manager Console中的每次更改后写下来。



PM > Update-Database -Force
Change AutomaticMigrationsEnabled = false; to AutomaticMigrationsEnabled = true;

and then after every changes in Package Manager Console write following.

PM > Update-Database -Force


这篇关于为什么代码首次迁移没有数据库中存在的更新表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆