The value cannot be null: connection during database testing

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

The error I get is

System.Data.Entity.Core.EntityException: The underlying provider failed on rollback. ---> System.ArgumentNullException: value cannot be null.
Parameter Name: Connection

The code is as follows:

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

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

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

PrimaryInitializerTest the class

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

PrimaryContextTest the class

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>();
    }
}

Connection string

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

Context line

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

What can i do wrong?

You may notice that the database does not exist in the folder App_Data...

+4
1

Commit, Rollback, .

, , ArgumentNullException SDK .

, Commit .

+2

Source: https://habr.com/ru/post/1622657/


All Articles