9

I'm 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 looks like this:

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 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 class

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

PrimaryContextTest 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 string

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

What could I be doing wrong?

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
KSHMR
  • 741
  • 1
  • 9
  • 24
  • When exactly does the error happen? When you start the app? – jpgrassi Jan 03 '16 at 23:10
  • When I run the test, and the program executes the Rollback function. Here are the variables if it helps http://i.imgur.com/ObGGgww.png – KSHMR Jan 03 '16 at 23:13
  • Though I'm not sure why you're getting a null reference exception, it doesn't really make sense to `Commit` and then immediately `Rollback` a transaction. It's definitely an error (SQL Server will also throw an error), though it shouldn't be a null error. – Rob Jan 03 '16 at 23:16
  • I'm new to working with Entity like this. Are the changes in context.Save() method actually saved? I thought they were just cached somehow. – KSHMR Jan 03 '16 at 23:19
  • Because when I just call Rollback() I don't see the Database variable change. I don't see it reverting the item I added to the database. – KSHMR Jan 03 '16 at 23:21
  • Well of course not. Once you committed the transaction, that's it, there's no transaction anymore to roll back. –  Jan 04 '16 at 07:24
  • What did you end up doing? I am getting this error when the Commit call fails. – boggy Mar 07 '18 at 19:46
  • See this: https://stackoverflow.com/questions/22486489/entity-framework-6-transaction-rollback – boggy Mar 07 '18 at 19:48
  • This error is caused generally due to some other failure! – Ashutosh Apr 20 '19 at 18:32

1 Answers1

8

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

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

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.

Luke Puplett
  • 42,091
  • 47
  • 181
  • 266