Configure EF to not drop any tables in migration

吃可爱长大的小学妹 提交于 2019-12-21 23:24:16

问题


I am using EF migrations to dynamically create a database model at run-time. This can happen many times, sometimes modifying existing tables and sometimes not. As every time i do a migration I don't want to create a massive context containing all the DbSets on the whole database I just want to include the tables I'm currently modifying.
I am doing the migration in the following way

        Type contextType = null; //This will be my dbcontext

        DbMigrationsConfiguration migratorConfig = new DbMigrationsConfiguration();

        migratorConfig.ContextType = contextType;
        migratorConfig.TargetDatabase = new DbConnectionInfo("ConnectionString", "System.Data.SqlClient");
        migratorConfig.AutomaticMigrationsEnabled = true;
        migratorConfig.AutomaticMigrationDataLossAllowed = false;
        migratorConfig.ContextKey = "key";
        migratorConfig.MigrationsAssembly = contextType.Assembly;

        DbMigrator dbMigrator = new DbMigrator(migratorConfig);
        dbMigrator.Update();   

Is there any way I can configure EF to not drop the tables that are not included on the context but were present on previous migrations? Using a different ContextKey each time is not an option because EF will complain that a certain object already exists.
Thanks in advance.


回答1:


You can employ migrator.GetDatabaseMigrations(); which returns the correct list of migrations already applied to the database. And use the following due to get the pending migrations:

var mg = new DbMigrator(configuration);
var mgpen = mg.GetPendingMigrations().ToList();

And you may wanna see the scripts by this:

var scriptor = new MigratorScriptingDecorator(mg);
string script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null);

Then try to see/compare them to meet your purpose. You may wanna customize Migrations History Table to meat this :)

Note: Renaming the ContextKey is useful to deal with managing multiple models per physical database instance. This is an ability in EF6 Migrations known as Multiple Contexts per Database. According to MSDN the ContextKey :

Gets or sets the string used to distinguish migrations belonging to the configuration from migrations belonging to other configurations using the same database. This property enables migrations from multiple different models to be applied to applied to a single database.




回答2:


One way to do it is to use manual migrations, but this would be a bit difficult to achieve, however if you have a larger database and have lower number of modifications, this should work.

use something like,

namespace MigrationsDemo.Migrations 
{ 
    using System; 
    using System.Data.Entity.Migrations; 

    public partial class AddBlogUrl : DbMigration 
    { 
        public override void Up() 
        { 
            AddColumn("dbo.Blogs", "Url", c => c.String()); 
        } 

        public override void Down() 
        { 
            DropColumn("dbo.Blogs", "Url"); 
        } 
    } 
}

Please find more information here



来源:https://stackoverflow.com/questions/28815309/configure-ef-to-not-drop-any-tables-in-migration

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