Code First Migrations: Making __MigrationHistory not a system table

https://blog.oneunicorn.com/2012/02/27/code-first-migrations-making-__migrationhistory-not-a-system-table/

Code First Migrations uses a table called __MigrationHistory as a place to store metadata about the migrations that have been applied to the database. Code First creates this table when it creates a database or when migrations are enabled. In addition, when running against Microsoft SQL Server, Code First will also mark the table as a system table. However, several times recently the question has come up of how to make the table a normal user table instead of a system table. This is pretty easy to do and this post describes how.

Migrations doesn’t actually care whether or not __MigrationHistory is a system table. Indeed, with some providers, such as SQL Server Compact, the table is never marked as a system table. The reason it is marked as a system table on SQL Server is simply to keep it out of the way such that it doesn’t clutter up the view of your normal tables.

However, sometimes having __MigrationHistory as a system table can be a problem. For example, current versions of Web Deploy don’t deal well with system tables. The Web Deploy team are working on supporting Migrations but until this work is released you may want to make __MigrationHistory a normal user table.

For new databases

One way to make sure that __MigrationHistory is not created as a system table is to override the Migrations SQL generator for SQL Server. This only works if you do it before the table has been created, since Code First only tries to mark the table as a system table as part of creating the table. In other words, this method is only usually suitable for new databases where you haven’t yet performed any migrations.

To override the SQL generator, create a class that derives fromSqlServerMigrationSqlGenerator and override the GenerateMakeSystemTable method so that it does nothing. For example:

public class NonSystemTableSqlGenerator : SqlServerMigrationSqlGenerator 
  { 
      protected override void GenerateMakeSystemTable( 
          CreateTableOperation createTableOperation) 
      { 
      } 
  }

Now set an instance of this new class in your Migrations Configuration:

public Configuration() 
  { 
      AutomaticMigrationsEnabled = false; 
      SetSqlGenerator("System.Data.SqlClient", new NonSystemTableSqlGenerator()); 
  }

For existing databases

If you have an existing __MigrationHistory table and want to make it non-system, then you’ll have to do some work in SQL. The following worked for me although there are plenty of other ways to write the SQL that would have the same end result:

SELECT * 
  INTO [TempMigrationHistory] 
  FROM [__MigrationHistory]

DROP TABLE [__MigrationHistory]

EXEC sp_rename ‘TempMigrationHistory’, ‘__MigrationHistory’

And that’s it—you don’t have to have __MigrationHistory as a system table if you don’t want.

Thanks, 
Arthur

时间: 2024-08-05 06:41:44

Code First Migrations: Making __MigrationHistory not a system table的相关文章

Code First Migrations更新数据库结构的具体步骤

我对 CodeFirst 的理解,与之对应的有 ModelFirst与 DatabaseFirst ,三者各有千秋,依项目实际情况自行选择. 1.开发过程中先行设计数据库并依此在项目中生成 *.dbml 或是 *.edmx 文件的,就是DatabaseFirst: 2.开发时先建立空的 *.edmx 文件,由此文件生成数据库的,就是ModelFirst: 3.使用 System.Data.Entity. DbContext 与 System.Data.Entity. DbSet构建数据模型,没有

Code First Migrations更新数据库结构(数据迁移) 【转】

背景 code first起初当修改model后,要持久化至数据库中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们的旧数据库中包含一些测试数据时,当持久化更新后,原数据将全部丢失,故我们可以引入EF的数据迁移功能来完成. 要求 已安装NuGet 过程示例 [csharp] view plaincopyprint? //原model //原model [csharp] view plaincopyprint? us

MVC学习6 学习使用Code First Migrations功能 把Model的更新同步到DB中

 参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table 转:http://www.it165.net/pro/html/201403/10653.html 本文内容: 1,学习Entity Framework Code First 迁移功能(Migrations) 2,更新Model Class(Model

EF Code First Migrations数据库迁移

http://www.cnblogs.com/libingql/p/3330880.html 1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramework. PM> Install-Package EntityFramework 安装成功后,界面提示如下图: 在新建的Portal控制台应用程序中添加两个实体类,代码结构如下: 其中,类文件PortalCo

【EF】EF Code First Migrations数据库迁移

1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramework. PM> Install-Package EntityFramework 安装成功后,界面提示如下图: 在新建的Portal控制台应用程序中添加两个实体类,代码结构如下: 其中,类文件PortalContext.cs的代码如下: using System; using System.Colle

Entity Framework Code First Migrations 官方Demo

一.建立初始模型和数据库 1.创建一个新的控制台程序 MigrationsDemo . 2.安装最新的 EntityFramework NuGet 程序包 打开程序包管理器控制台 -> 运行命令 Install-Package EntityFramework 3.创建名为 Model.cs 的文件,代码如下,其中定义了一个组成领域模型的 Blog 类,和一个 EF Code First 上下文类 BlogContext . 1 using System.Data.Entity; 2 using

EF6与MVC5系列(5):使用数据库迁移( Code First Migrations)和发布

本节教程包含以下内容: 启用数据库迁移(Code First Migrations):迁移特性可以改变数据模型,并且不需要删除重建数据库就可以修改数据库架构. 部署在Azure中:这步骤是可选的,可以不发布在Azure中继续学习本教程后面的内容. ps:本文中只翻译Code First Migrations相关内容,有关如何在Azure中发布,可以查看原文:https://www.asp.net/mvc/overview/getting-started/getting-started-with-

“Code First Migrations ”工具【转】

在本篇文章中,我们学习如何使用实体框架的"Code First Migrations "(也称为代码先行功能)工具,使用其中的"迁移"功能对模型类进行一些修改,同时同步更新对应数据库的表结构. 默认情况下,当您使用实体框架的"Code First Migrations "工具,实体框架会自动创建一个数据库."代码先行功能"首先会添加一个新表到数据库中,以便跟踪数据库的架构是否与模型类同步.如果它们不同步,实体框架会抛出一个错误

学习ASP.NET MVC(八)——“Code First Migrations ”工具

在本篇文章中,我们学习如何使用实体框架的“Code First Migrations ”(也称为代码先行功能)工具,使用其中的“迁移”功能对模型类进行一些修改,同时同步更新对应数据库的表结构. 默认情况下,当您使用实体框架的“Code First Migrations ”工具,实体框架会自动创建一个数据库.“代码先行功能”首先会添加一个新表到数据库中,以便跟踪数据库的架构是否与模型类同步.如果它们不同步,实体框架会抛出一个错误.这样开发人员在开发时就能更容易地追查问题出在什么地方,而其他的开发方