I'm trying to get the (by Entity Framework generated) table __EFMigrationsHistory
as an entity into my database context:
In Entity Framework 6 this worked fantastically, but did anyone tried it in EntityFramework 7?
What I tried is, to just add a new class __EFMigrationsHistory
- but it'd be too easy - there is already a table named __EFMigrationsHistory in the database
(thanks...)
Then I read about that HistoryRow, I shall inherit a class from. So I created the class
public class MigrationsHistory : HistoryRow
{
//... constructor etc.
}
started a migration and: I got two migration-history-tables (but only the original one works)
So I read on and searched for interfaces and classes to implement/inherit from. I found SqlServerHistoryRepository
- looks nice. I created an new database context inheriting from SqlServerHistoryRepository
(like I'd have done it in EF6). - But this is not a context-class, so I can not add it in Startup.cs (like I did with my default applicationcontext)
So I checked the DbContext
for maybe add the history-table somewhere (like in EF6), but there is nothing for adding the table to my context.
So: Anyone already tried to add the migration-history to his context? Anyone was successful?
Whilst I experimented with bricelam's answer with no success with EF Core 2.0, in Microsoft.EntityFrameworkCore there's a method;
// .NET Core 1.0 + Platform Extensions
// Microsoft.EntityFrameworkCore.Relational, Version=1.1.0.0, PublicKeyToken=adb9793829ddae60
namespace Microsoft.EntityFrameworkCore
{
public static class RelationalDatabaseFacadeExtensions
{
public static IEnumerable<string> GetAppliedMigrations(this DatabaseFacade databaseFacade);
}
}
Call it as follows;
var dbcontext = new DbContext();
IEnumerable<string> history = dbcontext.Database.GetAppliedMigrations();
Mapping to the table by name should work (it is that easy), you just need to ensure you don't try to re-create it (e.g. by accidentally calling DbContext.Database.EnsureCreate()
)
class MyContext : DbContext
{
public DbSet<AppliedMigration> AppliedMigrations { get; set; }
}
[Table("__EFMigrationsHistory")]
class AppliedMigration
{
[Column("MigrationId")]
public string Id { get; set; }
[Column("ProductVersion")]
public string EFVersion { get; set; }
}