实体框架和 UPDATE CASCADE
看到这里有点沮丧。我正在尝试找到一种支持实体框架更新级联的方法,但似乎没有内置的方法。网上的研究基本上表明每个人都说无论如何都不应该更改主键值,但在某些情况下您需要这样做(例如,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法直接通过 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.