您可以创建无EF5迁移数据库? [英] Can you create a database without migrations in EF5?

查看:210
本文介绍了您可以创建无EF5迁移数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的官方asp.net开始了解EF 5使用MVC 4。在这个教程中,当执行迁移(在我的理解)创建数据库。当我用的mvc 5教程看EF 5他们没有使用迁移创建一个数据库。他们使用的数据库初始化。所以,我在想,能在没有EF 5使用迁移为项目创建一个数据库?此外,将区别是什么用这两种方式?

I am following the offical asp.net "Getting started with EF 5 using MVC 4". In that tutorial, the database is created when the migrations are performed(in my understanding). When I was looking at the EF 5 with Mvc 5 tutorial they didn't use migrations to create a database. They use database initializer. So, I was wondering could create a database for your project without using migrations in EF 5? Also, what would the difference be with both these approaches?

推荐答案

code首先迁移和使用软件包管理器控制台命令来完成升级,可以先有点混乱。
您可以使用初始化为 CreateDatabaseIfNotExists DropCreateIfModelChanges DropCreateDatabaseAlways MigrateDatabaseToLatestVersion 结果
看到界面 IDatabaseInitializer< TContext方式>

Code first Migrations and using Package Manager Console Commands to do upgrades can get a bit confusing at first. You can use the initializer to CreateDatabaseIfNotExists , DropCreateIfModelChanges, DropCreateDatabaseAlways and to MigrateDatabaseToLatestVersion
See the interface IDatabaseInitializer<TContext>.

CreateDatabaseIfNotExists   // is the Default initializer.

所以这就是为什么它似乎EF少了点东西有时你。

So this is why it appears EF just does things for you sometimes.

因此​​,答案为是可以创建而不迁移数据库

但不同的是不明显的,如果你要那么做长期是另一个问题。
如果您正在使用的迁移。这将更新数据库到code第一种模式相匹配。
如果没有数据库,那么这意味着创建数据库。
所以,这就是为什么自动迁移和CREATEDB看起来很奇怪,因为它们可能会导致同样的结果有时。但在技术上,他们是不同的。

But the difference is not obvious and if you would do that long term is another question. If you are using migrations. It would Update the Db to match the code first model. If there is NO database, then that means creating the database. So Thats why Automated migrations and CreateDB look confusing since they can result in same outcome sometimes. But technically they are different.

所以一般就足够了先用code自动迁移而已。

So generally it is sufficient to use code first automatic "migrations" only.

迁移可以是自动或托管。
该管理办法的迁移产生invovles code,扭捏code和运行PM命令行或PowerShell命令来实际执行迁移。

Migrations can be either Automatic or "managed". The managed migrations approach invovles generating code , tweaking the code and running PM commandlet or POwershell command to actually perform the migration.

通过自动迁移,你只需要设置intitializer和访问的DbContext。

With Automated migrations you just need set the intitializer and Access the DBContext.

有2个部分的过程。

一)DB初始化程序的步骤。
立即实例YourDBContext之前做到这一点。

a) The DB Initializer step. do this immediately before instantiating YourDBContext.

 //eg
 // DONT TOUCH MY DB or i break your back!
 Database.SetInitializer(new ContextInitializerNone<YourDbContext>());  // Do Nothing, 
 // OR 
// yes migrate my db to match my code please.  
Database.SetInitializer(new MigrateDatabaseToLatestVersion<YourDbContext, YourMigrationConfiguration>());  // Set to migration is requested, see config class below

使用迁移初始化时指定的Confirguration类看起来像这样

The Confirguration class specified when using Migration initializer looks like this

public class YourMigrationConfiguration<TContext> : DbMigrationsConfiguration<TContext> 
    where TContext  : DbContext{

    protected  YourMigrationConfiguration() {
        AutomaticMigrationsEnabled = true;  // run it when needed. Do not wait for my PM Command
        AutomaticMigrationDataLossAllowed = true; // if the new db look  means dropping tables or columns go ahead and kill my data. So use this option with caution.

    }

在需要时

然后就引发code中的迁移。

then just trigger the migration in code when required.

    Context.Database.Initialize(true);    // i place this inside a method on my UoW class

<一个href=\"http://www.entityframeworktutorial.net/$c$c-first/database-initialization-strategy-in-$c$c-first.aspx\"相对=nofollow> code第一DB初始化策略。结果
code首先迁移推荐阅读结果
管理迁移

有关于这一主题在网络上的文章。

There are many articles on the web on this topic.

这篇关于您可以创建无EF5迁移数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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