模型更改后更新数据库 - 实体框架7

时间:2022-12-23 02:08:17

I have created an app using the lastest ASP.NET5 MVC 6 Entity Framework 7 and setup migrations using

我使用最新的ASP.NET5 MVC 6实体框架7创建了一个应用程序并使用设置迁移

dnx . ef migration add Initial
dnx . ef migration apply

This works but when I make a change to the model the database is not updated. I would like to have the database automatically update after a model change when I run the program.

这有效,但是当我对模型进行更改时,数据库不会更新。我想在运行程序时更改模型后自动更新数据库。

My research only points me to old information that doesn't seem to be appropriate to Entity Framework 7.

我的研究只向我指出了似乎不适合实体框架7的旧信息。

My current code:

我目前的代码:

 public ApplicationDbContext(): base()
   {

        if (!_created)
        {

             Database.AsRelational().ApplyMigrations();
             _created = true;         
        }
  }

Can someone point me in the right direction?

有人能指出我正确的方向吗?

I believe it use to work something like this:

我相信它可以用来做这样的事情:

Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());

2 个解决方案

#1


5  

You must manually run migrations with EF7 from command line, or call Database.Migrate from code, there is nothing automagic in EF7 (a deliberate decision) and after you change your model, create a new migration

您必须从命令行手动运行EF7迁移,或者从代码调用Database.Migrate,EF7中没有任何自动化(经过深思熟虑的决定),并且在更改模型后,创建新的迁移

#2


2  

There appears to be some confusion between the creation of migrations and the process of updating the database structure.

在创建迁移和更新数据库结构的过程之间似乎存在一些混淆。

In EF7, you can no longer automatically generate the migration (the delta between the current database structure and the entity definitions). That HAS to be done at the command line using the "migrations add" command.

在EF7中,您无法再自动生成迁移(当前数据库结构与实体定义之间的差异)。要使用“迁移添加”命令在命令行完成此操作。

Updating the structure of the database, however, can still be done via code. That is done using the dbContext.Database.Migrate() method. You can wire this up in your Startup so that when your app first boots it will ensure your database has been brought up to date with the now-current version of your app.

但是,更新数据库的结构仍然可以通过代码完成。这是使用dbContext.Database.Migrate()方法完成的。您可以在启动时将其连接起来,这样当您的应用程序首次启动时,它将确保您的数据库已与当前版本的应用程序保持同步。

So your development workflow can be:

所以你的开发工作流程可以是:

  1. modify entity definitions
  2. 修改实体定义

  3. run "migrations add" command
  4. 运行“迁移添加”命令

  5. launch your app*

    启动你的应用*

    • number 3 above assumes you've wired up the Migrate() call mentioned above in your Startup. Otherwise you also have to execute the "database update" command manually.
    • 上面的数字3假设您在启动时连接了上面提到的Migrate()调用。否则,您还必须手动执行“数据库更新”命令。

#1


5  

You must manually run migrations with EF7 from command line, or call Database.Migrate from code, there is nothing automagic in EF7 (a deliberate decision) and after you change your model, create a new migration

您必须从命令行手动运行EF7迁移,或者从代码调用Database.Migrate,EF7中没有任何自动化(经过深思熟虑的决定),并且在更改模型后,创建新的迁移

#2


2  

There appears to be some confusion between the creation of migrations and the process of updating the database structure.

在创建迁移和更新数据库结构的过程之间似乎存在一些混淆。

In EF7, you can no longer automatically generate the migration (the delta between the current database structure and the entity definitions). That HAS to be done at the command line using the "migrations add" command.

在EF7中,您无法再自动生成迁移(当前数据库结构与实体定义之间的差异)。要使用“迁移添加”命令在命令行完成此操作。

Updating the structure of the database, however, can still be done via code. That is done using the dbContext.Database.Migrate() method. You can wire this up in your Startup so that when your app first boots it will ensure your database has been brought up to date with the now-current version of your app.

但是,更新数据库的结构仍然可以通过代码完成。这是使用dbContext.Database.Migrate()方法完成的。您可以在启动时将其连接起来,这样当您的应用程序首次启动时,它将确保您的数据库已与当前版本的应用程序保持同步。

So your development workflow can be:

所以你的开发工作流程可以是:

  1. modify entity definitions
  2. 修改实体定义

  3. run "migrations add" command
  4. 运行“迁移添加”命令

  5. launch your app*

    启动你的应用*

    • number 3 above assumes you've wired up the Migrate() call mentioned above in your Startup. Otherwise you also have to execute the "database update" command manually.
    • 上面的数字3假设您在启动时连接了上面提到的Migrate()调用。否则,您还必须手动执行“数据库更新”命令。