Entity Framework 7 中 DbContext 中的 MigrationHistory 作为实体

MigrationHistory as entity in DbContext in Entity Framework 7

我正在尝试将(由 Entity Framework 生成的)table __EFMigrationsHistory作为一个实体放入我的数据库上下文中:

在 Entity Framework 6 中这非常有效,但是有人在 EntityFramework 7 中尝试过吗?

我尝试的是,只添加一个新的 class __EFMigrationsHistory - 但它太容易了 - there is already a table named __EFMigrationsHistory in the database(谢谢...)

然后我读到了那个 HistoryRow,我将从那里继承一个 class。所以我创建了 class

public class MigrationsHistory : HistoryRow 
{ 
    //... constructor etc.
}

开始迁移并且:我有两个迁移历史-tables(但只有原始的有效)

所以我继续阅读并搜索接口和 classes 到 implement/inherit。我发现 SqlServerHistoryRepository - 看起来不错。我创建了一个继承自 SqlServerHistoryRepository 的新数据库上下文(就像我在 EF6 中所做的那样)。 - 但这不是上下文 -class,所以我不能将它添加到 Startup.cs 中(就像我对默认应用程序上下文所做的那样)

所以我检查了 DbContext 是否可以在某处添加 history-table(比如在 EF6 中),但是没有任何东西可以将 table 添加到我的上下文中。

所以:有人已经尝试将迁移历史添加到他的上下文中了吗?有人成功了吗?

按名称映射到 table 应该可行( 就是这么简单),您只需要确保您不会尝试重新创建它(例如,不小心调用 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; }
}

虽然我在 EF Core 2.0 上尝试了 bricelam 的回答但没有成功, 在 Microsoft.EntityFrameworkCore有个方法;

// .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);
    }
}

调用如下;

var dbcontext = new DbContext();
IEnumerable<string> history = dbcontext.Database.GetAppliedMigrations();