自动迁移不起作用[EF Core] [英] Automatic migrations doesn't work [EF Core]

查看:62
本文介绍了自动迁移不起作用[EF Core]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在项目中自动进行迁移,所以增加了2行

I tried to automate migrations in my project so added a 2 lines

_context.Database.Migrate();
_context.Database.EnsureCreated;

但是我在数据库中看不到任何更新.为什么?

But i can't see any updates in my database. Why?

推荐答案

自动迁移已从EF Core中删除.EF Core 2.0中没有此功能.没有计划将此功能添加到EF.微软认为它的好处少于缺点.

Automatic migration has been removed from EF Core. This feature is not present in EF Core 2.0. There is no plan to add this feature to EF. Microsoft thinks it benefits is less than its drawbacks.

EF Core 2.0中有2种与迁移相关的方法. Database.Migrate() Database.EnsureCreated().它们都不是完整的迁移.

There are 2 methods in EF Core 2.0 related to migration. Database.Migrate() and Database.EnsureCreated(). Neither of them are a complete migration.

Migrate()不添加或创建迁移.它仅检查是否存在任何未应用的迁移.如果是,则根据它们更新数据库.

Migrate() does not add or create a migration. It only checks if any not-applied migrations exists or not. If yes, then updates the database based on them.

EnsureCreated()根据项目中的模型创建数据库.但是它不是以迁移的方式来完成的.实际上,此方法不需要迁移.此方法的缺点是由它创建的数据库将来无法通过任何迁移进行更新.确实,此方法已添加到EF中,以帮助人们以MVP风格快速创建项目.

EnsureCreated() creates the database based on the models in the project. But it does not do this in the migration way. Actually no migrations are needed by this method. Disadvantage of this method is that a database created by it, can not be updated in future by any migrations. Indeed this method is added to EF to help people create projects fast in MVP style.

结论:首先添加迁移,然后更新数据库(Database.Migrate())

Conclusion: First add migration and then do update database ( Database.Migrate())

这篇关于自动迁移不起作用[EF Core]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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