tags:

views:

77

answers:

4

I'm using the following LINQ-statement to update a table-entry with the current date/time:

MembershipClassesDataContext db = new MembershipClassesDataContext();
var tmp = db.drun_addqol_2_usrs.SingleOrDefault(y => !y.done.HasValue && y.UserId.Equals(Membership.GetUser().ProviderUserKey.ToString()));
tmp.done = DateTime.Now;
// at this point, tmp.done has the correct value!
// also the entry isn't null because I got the correct ID with tmp.UserId
// But db.GetChangeSet().Count is zero (0), so the value of "done" is changed but not commited to the ChangeSet?!
db.SubmitChanges();

"done" is a column with type DateTime and after the above query it shouldn't be null but should contain the actual date / time. But... it doesn't update and the value is still null.

Any ideas where my mistake is?

Thanks in advance!

A: 

Before you call db.SubmitChanges(), call db.GetChangeSet() to determine if L2S thinks it has any rows to update. If the changeset is empty then your SingleOrDefault() method must be returning nothing.

Randy Minder
I made some small changes to the code sample. "Tmp" is the correct entry. Also "tmp.done" isn't null after setting it to DateTime.now, but the changed entry is not saved to the DB because the ChangeSet is still empty.
Anheledir
Oh, I just noticed that "tmp" is not an entity that your DB context is aware of. It's an anonymous variable. You need to actually modify an entity tracked by the context.
Randy Minder
I used the anonymous tmp variable only to check the selected entry. The changed entry isn't saved even when I wrote:db.drun_addqol_2_usrs.SingleOrDefault(...).done = DateTime.Now;
Anheledir
+1  A: 

Is the object being returned from SingleOrDefault one that already exists in the DB, or a new default object? if it's a new then you have to add it to the dataset or .submitchanges will not save it.

Russell Steen
It's an existing object that already exists..
Anheledir
for the sake of argument try adding before your save. Nothing in your comments indicates that this object without a doubt already exists in the db. If this line works (or doesn't work) we can rule out this possibility.Add is slightly different for Linq2entities vs. linqtosql, if you tell me which you are using i can give an example.
Russell Steen
I'm using Linq2Sql - and won't the entry be duplicated if it already exists?
Anheledir
A: 

Stupid question - In the designer code for your drun_addqol_2_usrs, does the if statement for the setter evaluate to true (and call the property changed stuff)?

set
{
    if ((this._done != value))
    {
        this.OndoneChanging(value);
        this.SendPropertyChanging();
        this._done = value;
        this.SendPropertyChanged("done");
        this.OndoneChanged();
    }
}
Frank Tzanabetis
Sry for my late reply - I checked the setter and it's actually there and is calling all those stuff.. :(
Anheledir
A: 

I wonder if tmp is attached to your datacontext?

Try -

db.drun_addqol_2_usrs.Attach(tmp)

and then call

SubmitChanges()

it'll either work, or it'll throw an exception complaining that it can't attach an entity to a data context when it's already attached to a data context, or something like that.

Frank Tzanabetis