配置EF不降迁移任何表 [英] Configure EF to not drop any tables in migration

查看:336
本文介绍了配置EF不降迁移任何表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF迁移动态创建在运行时的数据库模式。这可能发生很多次,有时是修改现有的表有时不是。随着每一次我做一个移民,我不希望创建一个包含整个数据库我只是想包括我目前正在修改的表上的所有DbSets一个巨大的背景下。结果
我做迁移以下列方式

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();   



有没有什么办法可以配置EF不丢弃未包括在上下文中的表,但在场以前迁移?使用不同的 ContextKey 每次是不是一种选择,因为EF会抱怨说,某个对象已经存在。结果
先谢谢了。

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.

推荐答案

您可以使用 migrator.GetDatabaseMigrations(); 这已经返回迁移的正确列表应用到数据库。并使用以下因获得待定迁移:

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 :)

请注意:重命名 ContextKey 是应对每个物理数据库实例管理多个模型有用。这是一种能力的 EF6迁移的被称为每多个环境数据库。据的 MSDN ,从而 ContextKey

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.

这篇关于配置EF不降迁移任何表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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