Can you create a database without migrations in EF5?

和自甴很熟 提交于 2020-01-14 03:35:13

问题


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?


回答1:


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.

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

So the answer is "YES you can "Create a Database without Migrations"

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.

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

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.

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

There are 2 parts to the process.

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

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.

    }

then just trigger the migration in code when required.

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

Code first Db initialization strategies.
Code first migrations recommended reading
Managed Migrations

There are many articles on the web on this topic.



来源:https://stackoverflow.com/questions/20255489/can-you-create-a-database-without-migrations-in-ef5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!