使用Linq2SQL删除记录时出错 [英] Error deleting a record using Linq2SQL

查看:109
本文介绍了使用Linq2SQL删除记录时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近收到一个客户的错误报告,没有运气解决它.我希望有人能给我一些可能出问题的见解.

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.

错误似乎很简单:

Csla.DataPortalException:DataPortal.Delete失败(System.InvalidOperationException:序列在System.Linq.Enumerable.SingleOrDefault [TSource](IEnumerable`1源)中包含多个元素

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

这是我的DataPortal_Delete方法,该方法将FileId(PK)作为参数.

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();
        }
    }

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

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?

推荐答案

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

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.

要查看的另一件事是在后台生成的SQL.不确定会有所帮助,但不会造成伤害.我不知道您的数据模型,所以我无法像我想的那样理解您的代码.

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.

这篇关于使用Linq2SQL删除记录时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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