I am using TransactionScope to test database actions. This is the test class:
// Test class
private TransactionScope _transactionScope = null;
[TestInitialize]
public void Initialize()
{
_transactionScope = new TransactionScope();
}
[TestCleanup]
public void Cleanup()
{
if (_transactionScope != null)
{
_transactionScope.Dispose();
_transactionScope = null;
}
}
[TestMethod]
[DeploymentItem("Db.sdf")]
public void AddToPresentationsTest()
{
var item = TestItem();
var db = new DbEntities();
var target = new DatabaseController {Entities = db};
target.AddToItems(item);
var result = db.Items.Any(p => p.Text.Equals(item.Text));
Assert.IsTrue(result);
}
A TransactionScope is created before each test and is disposed after the test is complete. When AddToItems method is called I get the following error:
System.Data.EntityException: The underlying provider failed on Open. ---> System.InvalidOperationException: The connection object can not be enlisted in transaction scope.
DatabaseController has the following code:
// DatabaseController class
private DbEntities _entities;
public DbEntities Entities
{
get { return _entities ?? (_entities = new DbEntities());}
set { _entities = value; }
}
protected override void Dispose(bool disposing)
{
if (disposing && _entities != null)
{
_entities.Dispose();
}
base.Dispose(disposing);
}
public void AddToItems(Item item)
{
Entities.Items.Add(item);
Entities.SaveChanges();
}
I use Sql Server Compact 4.0. Could you please point out what am I doing wrong?