Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error deleting a record using 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.

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?

like image 782
Buddy Lee Avatar asked Jan 18 '10 21:01

Buddy Lee


1 Answers

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.

like image 72
Michael Maddox Avatar answered Oct 19 '22 15:10

Michael Maddox