SQL Server EF 6 Code First __MigrationHistory in dbo schema by default

nfg76nw0  于 2023-04-28  发布在  其他
关注(0)|答案(3)|浏览(94)

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

ddrv8njm1#

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)); 
    } 
}
ukxgm1gy

ukxgm1gy2#

You could try executing EXEC sys.sp_MS_marksystemobject __MigrationHistory in your seed method using context.Database.ExecuteSqlCommand();

gkl3eglg

gkl3eglg3#

Simply adding modelBuilder.HasDefaultSchema("YourSchema"); to your context's OnModelCreating will do the trick.

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

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

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.HasDefaultSchema("YourSchema");
        }

    }

相关问题