1

Using Linq To Sql Classes do not update the object in the context. I'm using a Context to get some records and update them.

In my DB I have one record that I want to update...

I get it and I update a field, but the object in the context doesn't change. How is that possible? My code looks like:

using(myPersonalModelContext ctx = new myContext())
{
    List<LogIn> notProcessedLogins = ctx.LogIns.Where(login => login.processed == false).ToList(); //Returns 1 record. Ok.
    foreach (LogIn notProcessedLogin in notProcessedLogins)
    {
        notProcessedLogin.processed = true;
    }
    int notProcessedLoginsCounter = ctx.LogIns.Where(login => login.processed == false).Count(); //Returns 1!!!
    ctx.SubmitChanges(); //The object in the context has no change, so it doesn't update nothing.
    int notProcessedLoginsCounter = ctx.LogIns.Where(login => login.processed == false).Count(); //Returns 1!!!
}

I'm following the documentation example and looks quite trivial, so I don't get it. Any idea?

In the autogenerated file, I see that is updating _processed properly:

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_processed", DbType="Bit")]
public System.Nullable<bool> processed
{
    get
    {
        return this._processed;
    }
    set
    {
        if ((this._processed != value))
        {
            this._processed = value;
        }
    }
}

I have the impression that the object that I'm updating is, for some reason, detached from the Context.

Mario Levrero
  • 3,345
  • 4
  • 26
  • 57
  • 1
    You haven't submitted your changes to the DB prior to the second query. – spender Sep 15 '16 at 09:06
  • @spender but that doesn't explain why its still unchanged afterwards. – Keyur PATEL Sep 15 '16 at 09:09
  • @spender, the counter is just to show that the context has nothing to change. Still returns 1 if I do again the query after the submitchanges. I updated the code. – Mario Levrero Sep 15 '16 at 09:09
  • 1
    can you use `ctx.LogIns.Attach(notProcessedLogin)` after you update the value inside the `foreach`, I am not sure if the `ToList()` is detaching the entries or not – Monah Sep 15 '16 at 09:19
  • 1
    Does your table have a primary key? Also does ctx.GetChangeSet().Updates indicate that there are any changes detected? – sgmoore Sep 15 '16 at 09:24
  • @HadiHassan, your comment put me on the correct way, because I was receiving an error about having a primary Key, as also commented sgmoore. Thanks, guys! – Mario Levrero Sep 15 '16 at 09:27
  • @sgmoore You are right. If you add your comment as an answer I will accept it. – Mario Levrero Sep 15 '16 at 09:28
  • @MarioLevrero shall I write my comment as answer? maybe other will get same your problem? – Monah Sep 15 '16 at 09:29

1 Answers1

-2

I know the reason.

When you call ToList() it actually creates a copy, so you did nothing to change the original values.

Try:

using(myPersonalModelContext ctx = new myContext())
{
    var notProcessedLogins = ctx.LogIns.Where(login => login.processed == false); 
    foreach (LogIn notProcessedLogin in notProcessedLogins)
    {
        notProcessedLogin.processed = true;
    }
    int notProcessedLoginsCounter = ctx.LogIns.Where(login => login.processed == false).Count(); 
    ctx.SubmitChanges(); 
    int notProcessedLoginsCounter = ctx.LogIns.Where(login => login.processed == false).Count();
}
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41
  • It creates a copy with references... Removing `ToList()` mantains the behaviour... GoTo http://stackoverflow.com/questions/2774099/tolist-does-it-create-a-new-list – Mario Levrero Sep 15 '16 at 09:15
  • 2
    Interesting, for me it used to not update the changes. Perhaps since I use Entity Framework. Nevertheless, I apologize for the misleading answer then, I'll try to figure out another solution. – Keyur PATEL Sep 15 '16 at 09:18