实体框架和 UPDATE CASCADE

发布于 2024-10-01 15:05:20 字数 322 浏览 0 评论 0原文

看到这里有点沮丧。我正在尝试找到一种支持实体框架更新级联的方法,但似乎没有内置的方法。网上的研究基本上表明每个人都说无论如何都不应该更改主键值,但在某些情况下您需要这样做(例如,UPC 值作为主键,并且 UPC 条形码现在变得越来越大,这意味着更新现有的并保持适当的外键关系)。

一种方法显然是利用 SavingChanges 事件,查看主键字段是否正在更改,如果是,则遍历导航属性并以这种方式更新子表。

从理论上讲,这是可行的。但这听起来很麻烦。有人有更好的主意吗?不敢相信比尔会把这些东西排除在框架之外,只是因为大多数人不这样做。 SQL Server 仍然支持它...

谢谢!

A bit frustrated over here. I'm trying to find a way to support update cascading with the entity framework, and there doesn't seem to be a built-in way. Research on the net basically shows everyone saying that you should never change a primary key value anyway, but there are valid cases where you would need to (e.g. a UPC value as a primary key, and UPC barcodes are now becoming larger which means updating the existing ones and maintaining proper foreign key relationships).

One approach is apparently to tap into the SavingChanges event, see if the primary key fields are changing, and if so traverse the navigation properties and update the sub tables that way.

This, in theory, would work. But it just sounds cumbersome. Anyone have a better idea? Can't believe Bill would have left this stuff out of the framework just because most people don't do it. SQL Server still supports it ...

Thx!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮光之海 2024-10-08 15:05:20

您无法直接通过 EF 更改主键。问题是 EF 在主键 = x 的地方进行了更改。所以你不能改变x。

您可以从 EF 执行一个存储过程来更新主键。

在使用条形码的情况下,我将有一个主键,它是一个自动增量数字,然后是一个条形码作为具有唯一索引的不同字段。

You cannot change the primary key directly via EF. The problem is that EF makes changes where primary key = x. So you cannot change x.

You could execute a stored procedure from EF that would update a primary key.

In your case with barcodes, I would have a primary key that is an auto increment number, then a barcode as a different field with a unique index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文