是否有其他选项可用于调试 Lightswitch 中的此删除问题
我在灯开关上发布了此问题论坛却一无所获。我想在放弃 Lightswitch 之前我应该再尝试一次。
我正在对具有明确定义的关系约束的 Sql Server 数据库实施删除。由于各种原因,我无法对这些关系指定级联删除。
我的方法似乎是正确的,因为我成功删除了一种实体类型。在实体的删除事件中,我将删除所有相关子项,如下所示:
entity.simpleChild.Delete();
foreach (var child in entity.complexChild)
{
((IEntityObject)child).Delete();
}
foreach (var child in entity.ChildWithGrandChildren)
{
foreach (var grandChild in child.Children)
{
((IEntityObject)grandChild).Delete();
}
child.Delete();
}
但是,我的实体类型之一失败了。该实体有孙子,但我也将删除它们。不仅如此,当我注释掉孙子的删除并尝试删除没有相关孙子的实体时,我得到了同样的错误。我按照与有效的 TSQL 脚本相同的顺序删除所有内容。然而,在运行时,我收到此错误:
操作失败:无法更改关系,因为一个或多个外键属性不可为空。当关系发生更改时,相关的外键属性将设置为空值。如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。
当我分析数据库时,我可以看到很多选择,但没有尝试删除,因此 Lightswitch 只是认为会有问题。我可以做什么来尝试找出问题出在哪里?从模型中删除各种相关实体不会产生任何不同的消息。
I posted this issue on the lightswitch forum and got nothing. I thought I'd try one more time before giving up on Lightswitch.
I'm implementing deletes against a Sql Server database with well defined relational constraints. For various reasons, I can't specify cascade delete on those relationships.
My approach seems correct because I am successfully deleting one entity type. In the entity's Deleting event, I'm deleting all related children like this:
entity.simpleChild.Delete();
foreach (var child in entity.complexChild)
{
((IEntityObject)child).Delete();
}
foreach (var child in entity.ChildWithGrandChildren)
{
foreach (var grandChild in child.Children)
{
((IEntityObject)grandChild).Delete();
}
child.Delete();
}
One of my entity types is failing, however. This entity has grandchildren, but I'm deleting those as well. Not only that, but when I comment out the deletion of grandchildren and attempt to delete an entity that has no related grandchildren, I get the same error. I'm deleting everything in the same order as a TSQL script that works. At run time, however, I get this error:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
When I profile the database, I can see lots of selects, but no attempts at deletion, so Lightswitch just thinks there will be a problem. What can I do to try to figure out where the problem is? Removing various related entities from the model never produces any different message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是一些示例代码,能够成功删除 VS LightSwitch 应用程序中的 3 部分关系:人 - 子 - 孙子。 SQL Server 表定义使用 DELETE NO ACTION,就像您的示例一样。
令我震惊的是,该代码的结构与您发布的示例代码不同。特别是,此代码有一个内部循环,用于从中间子实体中删除孙子实体。由于您没有内部循环,因此您必须采用不同的策略来删除孙子(这在示例中未显示),否则就是问题所在。
Here is some sample code that is able to successfully delete a 3 part relationship: Person - Child - Grandchild in a VS LightSwitch application. The SQL server table definitions are using DELETE NO ACTION just like your example.
I'm struck by the fact that this code has a different structure than the sample code you've posted. In particular, this code has an inner loop that deletes grandchildren from intermediate child entities. Since you don't have an inner loop, you must either have a different strategy for deleting grandchildren which isn't shown in your sample, or that is the problem.
我遇到了同样的问题,并发现另一个开发人员将代码放置在子项的删除和删除事件中,并且该代码导致了错误。
希望这可以帮助其他面临同样问题的人。
I had the same problem and discouvered that another developer placed code in the deleting and deleted events for the child and this code caused the error.
Hope this help others facing the same problem.