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. 【BZOJ 1030】【JSOI 2007】文本生成器 AC自动机+递推

    一直不理解到底怎么做啊,想了好久$TwT$ 最后终于明白了为什么找到第一个满足条件的$fail$就计算,因为避免重复,这个回答,,, 然后$root$下面要接上26个节点,这里26个字母中不在字典内的 ...

  2. 第一个PyQt程序

    这个程序虽然小,具备pyqt程序的皱型,可以作为一个模板使用了 #!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from PyQt5.QtW ...

  3. Java 代码编译和执行的整个过程

    Java 代码编译是由 Java 源码编译器来完成,流程图如下所示: Java 字节码的执行是由 JVM 执行引擎来完成,流程图如下所示: Java 代码编译和执行的整个过程包含了以下三个重要的机制: ...

  4. HashMap Hashtable区别

    http://blog.csdn.net/java2000_net/archive/2008/06/05/2512510.aspx 我们先看2个类的定义 public class Hashtable ...

  5. 根据位置信息提取 fasta 文件中的序列 -- extract fasta sequence by their position

    #!/usr/bin/env python # usages: python extract_seq_by_pos.py input.fasta id_start_end > result.fa ...

  6. watch监听 chechbox 全选

    // 监控全选checkbox的状态 $scope.$watch('AllCheck', function (newValue, oldValue) { // 第一次不执行 if (newValue ...

  7. NuGet命令行工具和可视化工具

    Nuget出了命令行工具之外,还有一个可视化工具,下载地址如下: 命令行(推荐官网下载,有中文提示!):https://dist.nuget.org/index.html 可视化(简单.方便.易用): ...

  8. C语言的时间函数

    下面是C语言的获取本地时间和构造时间进行格式化时间显示输出的相关函数:This page is part of release 3.35 of the Linux man-pages project. ...

  9. Discuz! X2.5 /source/class/helper/helper_seo.php Remote Code Execution Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 SEO模块中的preg_replace+修正符e+双引号引发的远程代码执 ...

  10. 心血来潮学python

    第一次见python就被吸引了,嗯,前面在linux下调试过一些小段代码.最近工作都在windows下面,也懒得换去unbuntu,所以想着在win下安装python. 之前不知道用什么方法装的pyt ...