模型更改后更新数据库 - Entity Framework 7 [英] Update Database after Model Changes - Entity Framework 7

查看:27
本文介绍了模型更改后更新数据库 - Entity Framework 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用最新的 ASP.NET5 MVC 6 Entity Framework 7 创建了一个应用程序,并使用

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

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.

我的研究只为我指出了似乎不适合 Entity Framework 7 的旧信息.

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

我当前的代码:

 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>());

推荐答案

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

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

这篇关于模型更改后更新数据库 - Entity Framework 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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