EF 6 Code First __MigrationHistory in dbo schema默认情况下 [英] EF 6 Code First __MigrationHistory in dbo schema by default

查看:166
本文介绍了EF 6 Code First __MigrationHistory in dbo schema默认情况下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一个实体框架的新手,当我第一次运行我的应用程序后登录数据库,当我看到__MigrationHistory表时,我有点困惑。


$ b $我现在明白了这个表的需要,但是不喜欢它在用户表中的标准dbo模式中,我认为它的含义和风险。



我的第一个想法是将其移动到系统文件夹。当在EF环境中研究如何实现这一点时,我可以找到的是如何将其从系统移动到dbo。



我现在得到默认情况下__MigrationHistory的感觉在系统文件夹中...是这种情况?



如何配置我的上下文来默认管理/引用系统文件夹中的迁移历史表? p>

这是我的上下文,我做错了什么或缺少一些配置?

  public class MyContext:DbContext,IDataContext 
{
public IDbSet< Entity>实体{get;组;

public MyContext()
:base(ConnectionString)
{

}

public new IDbSet< TEntity>设置< TEntity>()其中TEntity:class
{
return base.Set< TEntity>();
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove< PluralizingTableNameConvention>();
}
}


解决方案

是一种移动__MigrationHistory的技术。该表具有自己的上下文( System.Data.Entity.Migrations.History.HistoryContext ),您可以覆盖:

  public class MyHistoryContext:HistoryContext 
{
public MyHistoryContext(DbConnection dbConnection,string defaultSchema)
:base(dbConnection,defaultSchema)
{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity< HistoryRow>()。ToTable(tableName:MigrationHistory,schemaName:admin);
modelBuilder.Entity< HistoryRow>()。Property(p => p.MigrationId).HasColumnName(Migration_ID);
}
}

然后你需要注册它:

  public class ModelConfiguration:DbConfiguration 
{
public ModelConfiguration()
{
this.SetHistoryContext (System.Data.SqlClient,
(connection,defaultSchema)=> new MyHistoryContext(connection,defaultSchema));
}
}


I am new to Code first Entity framework, when logging into the database after running my app for the first time I got a little confused when I saw the "__MigrationHistory" table.

I now understand the need for this table, but do not like it being in the standard dbo schema within the user table, I think its obtrusive and a risk.

My first thought was to move it to the system folder. When researching how to achieve this within the EF context all I could find is how to move it from system to dbo.

I now get the feeling __MigrationHistory should by default be created within the system folder... is this the case?

How can I configure my context to manage/reference the migration history table within the system folder by default?

Here is my context, am I doing something wrong or missing some configuration?

public class MyContext : DbContext, IDataContext
{
    public IDbSet<Entity> Entities { get; set; }

    public MyContext()
        : base("ConnectionString")
    {

    }

    public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
    {
        return base.Set<TEntity>();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

解决方案

There is a technique for moving __MigrationHistory. That table has it's own context (System.Data.Entity.Migrations.History.HistoryContext) that you can override:

public class MyHistoryContext : HistoryContext 
{ 
    public MyHistoryContext(DbConnection dbConnection, string defaultSchema) 
        : base(dbConnection, defaultSchema) 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
        base.OnModelCreating(modelBuilder); 
        modelBuilder.Entity<HistoryRow>().ToTable(tableName: "MigrationHistory", schemaName: "admin"); 
        modelBuilder.Entity<HistoryRow>().Property(p => p.MigrationId).HasColumnName("Migration_ID"); 
    } 
} 

Then you need to register it:

public class ModelConfiguration : DbConfiguration 
{ 
    public ModelConfiguration() 
    { 
        this.SetHistoryContext("System.Data.SqlClient", 
            (connection, defaultSchema) => new MyHistoryContext(connection, defaultSchema)); 
    } 
} 

这篇关于EF 6 Code First __MigrationHistory in dbo schema默认情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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