Elisabeth
Elisabeth

Reputation: 21226

System.Data.EntityException :: "The underlying provider failed on Open." in integration test

I am trying to do an delete entity integration test on my generic repository delete method.

Thats my test delete method:

[Test]
public void Delete()
{
    // Arrange
    var user = new User() { FirstName = "test", LastName = "test" };
    var repository = new GenericRepository<User>(_context);
    repository.Add(user);
    _context.SaveChanges();

    // Act
    repository.Delete(user);
    _context.SaveChanges();            

    // Assert
    var deletedUser = _context.Users.Find(user.UserId);
    Assert.Null(deletedUser);
}

When I call the _context.SaveChanges after the Delete(user) I get an EntityException: saying:"System.Data.EntityException :: "The underlying provider failed on Open."

There is surely no problem in accessing my database because I have done an Add test before and I checked that the entity was inserted into the table.

What could cause this problem and why this misleading error message?

These are my 2 repository methods Add and Delete:

public void Update(T entity)
{
    DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
    if (dbEntityEntry.State == EntityState.Detached)
    {
        DbSet.Attach(entity);
    }
    dbEntityEntry.State = EntityState.Modified;
}

public void Delete(T entity)
{
    DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
    if (dbEntityEntry.State != EntityState.Deleted)
    {
        dbEntityEntry.State = EntityState.Deleted;
    }
    else
    {
        DbSet.Attach(entity);
        DbSet.Remove(entity);
    }
}

UPDATE

This is the error message of the inner exception:

Test 'TLP.DataAccess.UnitTests.GenericRepositoryTests.Delete' failed: System.Data.EntityException : The underlying provider failed on Open. ----> System.Data.SqlClient.SqlException : MSDTC on server 'LISA\SQLEXPRESS' is unavailable.

Upvotes: 0

Views: 4289

Answers (1)

Craig Stuntz
Craig Stuntz

Reputation: 126587

Look at the InnerException property of the EntityException. This will give you details as to why the open failed.

Upvotes: 1

Related Questions