Hi all,
While developing & learning with Entity Framework, I'm having a curious problem with inserts when I run the tests for one entity in my model. In concrete, the problem is when running some tests together. I'll explain myself:
I have one entity in my model called "DtoCategoria", with 2 members: id:Int32 and name:string, mapped to one table where id is an identity column. This is ok and quite simple.
I have a Data Access Layer for this DTO, called CadCategoria, where I have created a method for making inserts, like:
public class CadCategoria
{
protected readonly CUENTASEntities bd = Singletons.bd;
public bool add(EntityObject entity)
{
try
{
bd.AddObject(EntitySet, entity);
return bd.SaveChanges() > 0;
}
catch (Exception e)
{
bd.Detach(entity);
return false;
}
}
// and some other methods... update, delete, etc.
}
and some other generic methods as well for updates and deletes. I share the context through the tiers in the app with a singleton pattern, like this:
public class Singletons
{
public static CUENTASEntities bd;
static Singletons()
{
bd = new CUENTASEntities();
}
}
This looks fine since now. But, I created some unit tests to see if every thing is allright, which look like:
[TestClass]
public class CadCategoriaTest
{
private CadCategoria bd = new CadCategoria();
[TestMethod]
public void addTest()
{
var catAdd = new DtoCategoria { name= "cat 1" };
Assert.IsTrue(bd.add(catAdd));
bd.delete(catAdd);
}
[TestMethod]
public void deleteTest()
{
var catDel = new DtoCategoria { name= "cat 2" };
bd.add(catDel);
Assert.IsTrue(bd.delete(catDel));
Assert.IsFalse(bd.delete(new DtoCategoria { name= "not exists", id = -1 }));
}
[TestMethod]
public void updateTest()
{
var a = new DtoCategoria { name= "cat 3" };
bd.add(a);
a.nombre = "name modified";
Assert.IsTrue(bd.update(a));
var b = bd.get(-1);
Assert.IsFalse(bd.update(b));
bd.delete(a);
}
}
and now comes the extrange thing:
- When running the update test alone: Tests passed
- When running update & add tests: Tests passed
- When running update & delete tests: Update test failed and delete passed!
The error is in the 2nd line of the test: bd.add(a); that raises a OptimisticConcurrencyException in the SaveChanges method of the context.
Any idea why I have a concurrent exception with an insert with an identity column?? And only when combined with the delete test method?? It does not happen when combined with the add test, which also performs an "add" operation??
I'm afraid that if this fails in a test, it will also likely fail in the real app. It does not happen with another DTO which does not have an identity column id as PK.
Any ideas? Thanks a lot!!!
Sergi