开发者

Entity Framework delete object - concurrency error (how to disable concurrency)?

开发者 https://www.devze.com 2023-02-24 23:01 出处:网络
Is there a way to disable 开发者_Python百科concurrency error thrown in EntityFramework? For example:

Is there a way to disable 开发者_Python百科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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消