InMemory.TransactionManager中的get_CurrentTransaction没有实现

时间:2017-02-22 02:28:00

标签: entity-framework asp.net-core

我正在开发一个.NET Core应用程序,它实现了InMemoryDatabase以进行集成测试。我按照this link的说明操作。

基本上它显示了如何使用AddDbContext方法配置服务(在Startup.cs中),这就是我所做的,并且我调用了UseInMemoryDatabase:

    public void ConfigureServices(IServiceCollection services)
    {        
        services.AddDbContext<AppDbContext>(options => options.UseInMemoryDatabase());

        services.AddMvc();

        services.AddSingleton<ITodoRepository, TodoRepository>();
        services.AddScoped<IAssetRepository, AssetRepository>();
    }

我的AppDbContext在解决方案的另一个项目中定义,并且定义如下:

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> dbContextOptions) :
        base(dbContextOptions)
    {            
    }

    public DbSet<Asset> Assets { get; set; }
    public DbSet<TodoItem> Todos { get; set; }
}

回到Startup.cs文件,根据我需要编码Configure方法的链接,如下所示:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();

            var repository = app.ApplicationServices.GetService<IAssetRepository>();
            InitializeDatabaseAsync(repository).Wait();
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

此代码段运行正常并执行对GetService的调用。此调用运行AppDbContext的构造函数,但构造函数与异常异常崩溃。它在调用基础构造函数时崩溃:base(dbContextOptions)。

  

类型中的方法'get_CurrentTransaction'   'Microsoft.EntityFrameworkCore.Storage.Internal.InMemoryTransactionManager'   从程序集'Microsoft.EntityFrameworkCore.InMemory,   Version = 1.0.1.0,Culture = neutral,PublicKeyToken = adb9793829ddae60'   没有实施。

我似乎无法弄清楚导致此异常的原因。有人可以给我指点吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

在进行了一些调查之后,我发现发生了这个错误,因为包含配置代码(Startup.cs)的项目引用了“Microsoft.EntityFrameworkCore.InMemory”的版本1.0.1。

然而,我把它改为1.1.0,它突然起作用了。所以我假设实现已添加到1.1.0版本。

希望这有助于将来的任何人。