tags:

views:

46

answers:

2

I am new to Linq to SQL, but I am surprised at the problems I am having updating a table. From reading various sources I think the problem I get is a problem with the ORM mapping, but even so, given I am using VS 2008 and creating my dbml via a LINQ to SQL class, I do not expect this. So what is happening is that when I update and/or insert a row, lots of other rows get created in the table as well. I cannot predict what the pattern is when this happens, sometimes it doesn't happen. I am not sure the code below says very much about what the problem is, but I reproduce it here;

    public static void UpdateDailyTimeRecorded(
        int dailyTimeRecordedId, bool amFlag, string timeIn, string timeOut)
    {
        DailyTimeRecorded dtr = GetDailyTimeRecorded(dailyTimeRecordedId);
        if (amFlag == true)
        {
            dtr.MorningTimeIn_HH = Convert.ToInt32(timeIn.Substring(0, 2));
            dtr.MorningTimeIn_MM = Convert.ToInt32(timeIn.Substring(3, 2));
            dtr.MorningTimeOut_HH = Convert.ToInt32(timeOut.Substring(0, 2));
            dtr.MorningTimeOut_MM = Convert.ToInt32(timeOut.Substring(3, 2));
            dtr.MorningLeaveFlagId = 0;
        }
        else
        {
            dtr.AfternoonTimeIn_HH = Convert.ToInt32(timeIn.Substring(0, 2));
            dtr.AfternoonTimeIn_MM = Convert.ToInt32(timeIn.Substring(3, 2));
            dtr.AfternoonTimeOut_HH = Convert.ToInt32(timeOut.Substring(0, 2));
            dtr.AfternoonTimeOut_MM = Convert.ToInt32(timeOut.Substring(3, 2));
            dtr.AfternoonLeaveFlagId = 0;
        }
        try
        {
            db.SubmitChanges();
        }
        catch (ChangeConflictException)
        {
            db.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
        }
    }

I put a breakpoint on the line db.SubmitChanges(); and the rows get inserted at this point for sure, not before and not some code afterwards.

+1  A: 

Call DataContext.GetChanges() and see what else is captured. It might be from a previous 'non-committed' action on the data context.

leppie
It is indeed. I have 20 inserts. No doubt the reason why is because my DataContext field is stored as follows;public static FWHDataContext db = new FWHDataContext();I put in the folowing code; ChangeSet cs = db.GetChangeSet(); db.GetChangeSet().Inserts.Clear(); cs = db.GetChangeSet();Initially to put in breakpoints and see what was happening.HOwever clearing the inserts had unexpected knock on effects. What is the optimal solution for this?
arame3333
Why is the DataContext static? This seems dangerous. DataContexts are designed to be lightweight so you can create them wherever you need. It's better to create and destroy them a bunch of times than to keep one lying around for everything.
roufamatic
You are right it should not be static.I have changed that now.
arame3333
+1  A: 

One cause that I'm aware of off the top of my head that might cause LINQ to insert unexpected data is this. Even though you don't explicitly add a row of data to a particular table, LINQ to SQL will implicitly add it if you have it linked to another object that you have explicitly added.

Another cause is that you cannot readily rollback or undo pending changes added to a context. My understanding is that the best practice is to dispose of the context containing the unwanted objects and start a new one if and when you realize that you didn't want to perform the update.

BlueMonkMN
This is helpful.I now understand DataContext much better now.
arame3333