在实体框架唯一约束交换价值 [英] Swapping values with unique constraint in Entity Framework

查看:123
本文介绍了在实体框架唯一约束交换价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导航表的列名为首页的唯一约束。我有两个导航的实体,我想交换的首页值。

I have a unique constraint on a Navigations table's column called Index. I have two Navigation entities and I want to swap their Index values.

当我称之为 db.SaveChanges 它抛出一个异常,表明一个独特的约束被侵犯。它似乎EF被更新一个值,然后其他的,从而侵犯该约束。

When I call db.SaveChanges it throws an exception indicating that a unique constraint was violated. It seems EF is updating one value and then the other, thus violating the constraint.

难道不应该是事务更新他们两个,然后试图就提交的值进行排序并没有违反约束?

Shouldn't it be updating them both in a transaction and then trying to commit once the values are sorted out and not in violation of the constraint?

有没有办法解决,而无需使用临时值?

Is there a way around this without using temporary values?

推荐答案

这不是问题EF但SQL数据库的问题是由于更新指令被依次执行。交易无关这个 - 所有的约束不按每笔交易命令时验证。如果你想交换你需要更多的步骤,您将使用更多的虚拟值,以避免这种情况的唯一值。

It is not problem of EF but the problem of SQL database because update commands are executed sequentially. Transaction has nothing to do with this - all constrains are validated per command not per transaction. If you want to swap unique values you need more steps where you will use additional dummy values to avoid this situation.

这篇关于在实体框架唯一约束交换价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆