What is a common practice when trying to do Inserts and Deletes with Entity Framework and Unit Of Work, all in the same commit?

StackOverflow https://stackoverflow.com/questions/4779978

Question

I've implimented a Unit of Work pattern with my Entity Framework repository. ** golf clap **

Now i'm wondering what the common practice is, for doing multiple insets and/or deletes all within the same commit?

for example: Imagine I wish add 5 new objects/entities and for some reason, now delete entity #3.

// Arrange.
var uow = new UnitOfWork() { ... };
var myRepository = new MyRepository(uow);
var entity1 = new Entity(){ ... };
// ... snip snip ..

// Acts.
myRepository.Add(entity1);
myRepository.Add(entity2);
myRepository.Add(entity3);
myRepository.Add(entity4);
myRepository.Add(entity5);

 // do some logic ...

myRepository.Delete(entity3);

uow.Commit();

Will this work .. as in .. should the entities be added and THEN 1 removed from the db? Or will this only happen against the EF Entities list?

Is this bad practice? ie. never mix adds/updates with deletes? always commit before a delete?

Was it helpful?

Solution

It's allowed, but the order isn't guaranteed. EF will try to figure out the best order, and throw if it can't. If this sounds hard to test comprehensively, well, you're probably right.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top