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

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

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

    一.打开程序包管理器控制台 当你的实体模型与数据库架构不一致时,引发以下错误:The model backingthe 'SchoolContext' context has changed sinc ...

  2. 转载Code First Migrations更新数据库架构的具体步骤

    [转载] Code First Migrations更新数据库结构的具体步骤 我对 CodeFirst 的理解,与之对应的有 ModelFirst与  DatabaseFirst ,三者各有千秋,依项 ...

  3. EF Code First Migrations数据库迁移

    1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...

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

    在本篇文章中,我们学习如何使用实体框架的“Code First Migrations ”(也称为代码先行功能)工具,使用其中的“迁移”功能对模型类进行一些修改,同时同步更新对应数据库的表结构. 默认情 ...

  5. ASP.NET 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-th ...

  6. Entity Framework Code First -- Migrations 迁移

    在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个新的控制台应用程序 M ...

  7. C# EF Code First Migrations数据库迁移

    1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...

  8. EF Code First Migrations数据库迁移 (转帖)

    1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...

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

    1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...

随机推荐

  1. python之旅【第一篇】

    python简介 python的起源 追溯Python语言的起源,是从20世纪90年代初由Guido van Rossum,在阿姆斯特丹,开发一个新的脚本解释程序.不知道Guido当初有没有想到,Py ...

  2. 我的防Q+

    Q+链接:  http://onemore.web-45.com/index1.html: 兼容IE8: __页面被主机屋收回去了,现在又在弄自己的服务器,稍等呗

  3. CSS3动画属性之Animation

    首先定义一个动画规则: @keyframes mymove { from {top:0px;} to {top:200px;} } @-moz-keyframes mymove /* Firefox ...

  4. sql insert into select语句写法-将查询结果直接插入到表中

    insert into month_gpcj_info(idStr,zszrmygpsl,xyzrmygpsl,mycje,mycjl,month_date,dataCompiledDate) sel ...

  5. Ehcache缓存配置

    Cache的配置很灵活,官方提供的Cache配置方式有好几种.你可以通过声明配置.在xml中配置.在程序里配置或者调用构造方法时传入不同的参数. 你可以将Cache的配置从代码中剥离出来,也可以在使用 ...

  6. 【收藏】Android AutoLayout全新的适配方式, 堪称适配终结者

    来源:http://blog.csdn.net/lmj623565791/article/details/49990941 更多:Android屏幕适配全攻略(最权威的Google官方适配指导) 一. ...

  7. 查看mysql语句运行时间

    show profiles 之类的语句来查看 mysql> show profiles; Empty set mysql> show variables like "%pro%& ...

  8. R 绘图 填充颜色

    d <- density(mtcars$mpg) plot(d, main="Kernel Density of Miles Per Gallon") polygon(d, ...

  9. 精通Web Analytics 2.0 (3) 第一章:网站分析的新奇世界

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第一章:Web Analytics 2.0的新奇世界 多年以来,我们很清楚的知道,网站分析能够真正的改革网络上业务的完成方式.那 ...

  10. [bzoj4552][Tjoi2016][Heoi2016]排序

    Description 给出一个$1$到$n$的全排列,现在对这个全排列序列进行$m$次局部排序,排序分为$2$种: $1.(0,l,r)$表示将区间$[l,r]$的数字升序排序; $2.(1,l,r ...