Error deleting a record using Linq2SQL

寵の児 提交于 2019-12-10 13:36:03

问题


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?


回答1:


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.




回答2:


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.




回答3:


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)



来源:https://stackoverflow.com/questions/2089363/error-deleting-a-record-using-linq2sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!