0

Is there a way to disable concurrency error thrown in EntityFramework?

For example:

using (xEntities5 entities = new xEntities5()) { entities.Domains.MergeOption = System.Data.Objects.MergeOption.NoTracking; Domain domain = new Domain() { DomainId = id }; EntityKey key = entities.CreateEntityKey(entities.CreateObjectSet<Domain>().EntitySet.Name, domain); domain.EntityKey = key; entities.Attach(domain); //entities.AttachTo(entities.CreateObjectSet<Domain>().EntitySet.Name, domain); entities.DeleteObject(domain); return entities.SaveChanges(); // returns affected rows... must catch error? } 

Is there a way to not have to do try/catch around SaveChanges in order to detect if nothing was deleted?

2 Answers 2

2

As I know, you can't turn off concurrency error. Concurrency error is based on number of affected rows so if you want to delete a row and it is not deleted (for example because it doesn't exist any more) concurrency exception is fired. Moreover SaveChanges works in transaction so if you want to delete 5 rows and only 4 rows are deleted the exception is fired and all deletes are rolled back.

Concurrency test can be even more restrictive if you use columns marked as ConcurrencyMode.Fixed. These columns are used in where condition of SQL statements so only unmodified database records can be processed.

Once you get concurrency exception you are supposed to solve it.

Sign up to request clarification or add additional context in comments.

Comments

1

SaveChanges() will throw exceptions and doesn't catch them internally, so if you want to continue execution, you have to use try/catch. SaveChanges() also returns a method with the number of entities committed to the database, that is, if no error occurred :-)

HTH.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.