使用 Linq2SQL 删除记录时出错

发布于 2024-08-18 10:54:23 字数 1117 浏览 4 评论 0原文

我最近收到了客户的错误报告,但没有解决它。我希望有人能给我一些可能出错的见解。

该错误似乎很简单:

Csla.DataPortalException: DataPortal.Delete failed (System.InvalidOperationException: Sequence contains more one element at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)

这是我的 DataPortal_Delete 方法,它采用 我检查的

private void DataPortal_Delete(SingleCriteria<File, Guid> criteria)
    {
        using (var ctx = ContextManager<Ronin.Data.RoninDataContext>
                    .GetManager(Database.ApplicationConnection, false))
        {
            var data = ctx.DataContext.Files
                    .Single(row => row.FileId == criteria.Value);

            ctx.DataContext.FileSources.DeleteAllOnSubmit(data.FileSources);

            ctx.DataContext.Files.DeleteOnSubmit(data);

            ctx.DataContext.SubmitChanges();
        }
    }

第一件事是查看是否有另一个具有相同 FileId 的记录(尽管是主键,但这应该是不可能的)我启动的应用程序连接实际上是唯一的。到客户端数据库并尝试删除该记录,并且没有任何问题。客户端站点的 IT 人员使用“问题步骤记录器”向我发送了用户所采取操作的逐步屏幕截图。很普通,当他使用另一台机器时,他能够毫无错误地删除记录。显然,只有当应用程序在 Windows 7 中运行时才会发生这种情况。

也就是说,有什么想法可能会导致这种情况吗?

I've received an error report from a client recently and am having no luck resolving it. I'm hoping someone can give me some insight to what may be wrong.

The error seems simple enough:

Csla.DataPortalException: DataPortal.Delete failed (System.InvalidOperationException: Sequence contains more than one element at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)

Here is my DataPortal_Delete method, which takes the FileId (PK) as a parameter.

private void DataPortal_Delete(SingleCriteria<File, Guid> criteria)
    {
        using (var ctx = ContextManager<Ronin.Data.RoninDataContext>
                    .GetManager(Database.ApplicationConnection, false))
        {
            var data = ctx.DataContext.Files
                    .Single(row => row.FileId == criteria.Value);

            ctx.DataContext.FileSources.DeleteAllOnSubmit(data.FileSources);

            ctx.DataContext.Files.DeleteOnSubmit(data);

            ctx.DataContext.SubmitChanges();
        }
    }

First thing I check was to see if there was another record with the same FileId (although being the primary key, this should be impossible). All FileIds were in fact unique. I launched the application connecting to the client database and tried to delete the record and it worked without any issues. The IT guy at the client site used the "Problem Step Recorder" to send me step by step screenshots of the actions taken by the user. Nothing out of the ordinary, and when he used a different machine, he was able to delete the record without any errors. Apparently this only happens when the application is run in Windows 7.

That said, any ideas as to what could be causing this?

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

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

发布评论

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

评论(3

一场信仰旅途 2024-08-25 10:54:23

假设对 Single 的调用是问题的根源,而不是:

ctx.DataContext.Files.Single(...)

更改代码以允许从该查询返回多行,然后在返回多行时记录它返回的内容。这应该会指出您的“重复”数据问题。

另一件需要注意的事情是在幕后生成的 SQL。不确定这会有所帮助,但不会造成伤害。我不知道你的数据模型,所以我无法像我想的那样理解你的代码。

Assuming the call to Single is the source of the problem, instead of:

ctx.DataContext.Files.Single(...)

change the code to allow the return of multiple rows from that query and then log what it's returning when it returns more than one row. This should point you toward your "duplicate" data problem.

Another thing to look at is the SQL that is being generated behind the scenes. Not sure that will help, but it can't hurt. I don't know your data model, so I can't understand your code as well as I would like to.

生生漫 2024-08-25 10:54:23

如果仅发生在 Windows 7 中,则这可能是由操作系统引起的。你在Vista上试过吗? Vista的环境与Windows 7类似。您也可以使用Windows Virtual PC + XP模式。这是一款专为 Windows 7 设计的虚拟化应用程序,让用户可以像在 Windows XP 中一样运行应用程序。注意:XP 模式需要支持虚拟化的处理器。

If it only happens in Windows 7 then this might be cause by the OS. Have you tried it on Vista? Vista's environment is simmilar on Windows 7. You can also use Windows Virtual PC + XP Mode. This is a virtualization application specially designed for Windows 7 to let users run applications like they use to in Windows XP. Note: XP Mode requires virtualization capable processor.

痴者 2024-08-25 10:54:23

删除一个实体时,我遇到了同样的异常。事实证明,问题出在 dbml 文件中定义的外键关系。所以这就是我的案例中例外的原因。
删除后,它可以删除记录(并且我不想级联删除另一个表中的记录,我只需要了解如何配置 linq-to-sql 以将外键列设置为 null )

I had the same exception when deleting one entity. The problem turned out to be a foreign key relation defined in the dbml-File. So this was the reason for the exception in my case.
After I removed that it worked to delete the record (and I didn't want to cascade delete the records from the other table, I just need to find out how to configurate linq-to-sql to just set the foreign key column to null)

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