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

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

问题描述

我使用最新的ASP.NET5 MVC 6实体框架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.Mgr从代码迁移,在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天全站免登陆