值不能为空:测试数据库时进行连接 [英] Value cannot be null: connection while testing database

查看:201
本文介绍了值不能为空:测试数据库时进行连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Entity Framework 6为我的ASP.NET MVC应用程序运行集成测试.

I'm trying to run integration tests for my ASP.NET MVC application using Entity Framework 6.

我得到的错误是

System.Data.Entity.Core.EntityException:基础提供程序在回滚时失败. ---> System.ArgumentNullException:值不能为null.
参数名称:connection

System.Data.Entity.Core.EntityException: The underlying provider failed on Rollback. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: connection

代码如下:

Database.SetInitializer(new PrimaryInitializerTest());
_context = new PrimaryContextTest();
_context.Database.Initialize(true);

using (var dbt = _context.Database.BeginTransaction())
{
     dbt.Commit();
     dbt.Rollback();
}

我还尝试过在using语句下进行dbt.UnderlyingTransaction.Connection.Open()调用,并在对Rollback()的调用下进行dbt.UnderlyingTransaction.Connection.Close()调用.那给了我错误Connection is not closed.

I also tried having an dbt.UnderlyingTransaction.Connection.Open() call just below the using statement, and a dbt.UnderlyingTransaction.Connection.Close() call just below the call to Rollback(). That gave me the error Connection is not closed.

PrimaryInitializerTest

protected override void Seed(PrimaryContextTest context)
{
    // (...) Input some values
    base.Seed(context);
}

PrimaryContextTest

public class PrimaryContextTest : DbContext
{
    public PrimaryContextTest() : base("PrimaryContextTest")
    {
        Database.SetInitializer(new DropCreateDatabaseAlways<PrimaryContextTest>());
    }

    public DbSet<Story> Stories { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
    }
}

连接字符串

<add name="PrimaryContextTest" 
     connectionString="Data Source=(LocalDb)\mssqllocaldb;Initial Catalog=PrimaryContextTest;Integrated Security=SSPI;AttachDbFilename=|DataDirectory|\PrimaryContextTest.mdf" 
     providerName="System.Data.SqlClient" />

上下文字符串

<context type="fcon.DAL.Tests.PrimaryContextTest, fcon, Version=1.0.0.0, Culture=neutral">
    <databaseInitializer type="fcon.DAL.Tests.PrimaryInitializerTest, fcon" />
</context>

我可能做错了什么?

可能会提到App_Data文件夹中不存在该数据库...

Might mention that the database doesn't exist in the App_Data folder...

推荐答案

您先呼叫Commit,然后呼叫Rollback,但是注释指出了这个错误.

You're calling Commit and then Rollback, but the comments point that mistake out.

该错误不是很直观,我的意思是,ArgumentNullException绝不应该从堆栈中移出SD​​K.

The error isn't very intuitive, I mean, an ArgumentNullException should never work its way out of an SDK from down the stack.

但是当我不小心重用同一事务实例,或者两次调用Commit,或者试图在分层错误恢复逻辑中回滚两次时,我就遇到了这个问题.

But I've had this when I've accidentally reused the same transaction instance, or called Commit twice, or tried to rollback twice in layered error recovery logic.

这篇关于值不能为空:测试数据库时进行连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆