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>();
}
}
3条答案
按热度按时间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:
Then you need to register it:
ukxgm1gy2#
You could try executing
EXEC sys.sp_MS_marksystemobject __MigrationHistory
in your seed method usingcontext.Database.ExecuteSqlCommand();
gkl3eglg3#
Simply adding
modelBuilder.HasDefaultSchema("YourSchema");
to your context'sOnModelCreating
will do the trick.