MigrationHistory as Entity Framework 7中的DbContext中的实体

时间:2016-10-31 21:53:51

标签: c# entity-framework entity-framework-core ef-migrations .net-core

我试图将(通过实体框架生成的)表__EFMigrationsHistory作为实体添加到我的数据库上下文中:

enter image description here

在实体框架6中,这非常有效,但有没有人在EntityFramework 7中尝试过它?

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

然后我读到了HistoryRow,我将继承一个类。所以我创建了这个类

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

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

所以我读了并搜索了实现/继承的接口和类。我发现SqlServerHistoryRepository - 看起来不错。我创建了一个继承自SqlServerHistoryRepository的新数据库上下文(就像我在EF6中完成的那样)。 - 但这不是一个上下文类,所以我不能在Startup.cs中添加它(就像我使用我的默认applicationcontext一样)

所以我检查DbContext是否可以在某处添加历史表(如在EF6中),但没有任何内容可以将表添加到我的上下文中。

那么:有人已经尝试将迁移历史记录添加到他的上下文中吗?有人成功吗?

2 个答案:

答案 0 :(得分:2)

虽然我尝试使用bricelam的答案而EF Core 2.0没有成功, 在 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();

答案 1 :(得分:0)

按名称映射到表应该有效( 那么简单),你只需要确保你不要尝试重新创建它(例如,不小心调用{{1 }})

forty - two