开发者

Update table with LINQ

SOLVED look below code snippet.

I have a problem with updating tables, The code shows no faults but it don't act as expected, it does not put any information in the Log Table.

some explenation, i have a table called User with a FK on LogID and a table called Log with PK on LogID so that should be correct, the Log has a column called TimesLogg开发者_开发百科edIn and one called LastLogedin. and i want to update them accordingly.

        User logid = db.Users.Single(p => p.UserID == loginID);
        logid.Log.LastLogedin= DateTime.UtcNow;

        if (logid.Log.TimesLoggedIn == null)
        {
            logid.Log.TimesLoggedIn = 1;
        }
        else
        {
            logid.Log.TimesLoggedIn = logid.Log.TimesLoggedIn + 1;
        }
    db.SubmitChanges();

had an embarrassing fault in my code, i had

Response.Redirect("ControlPanel");

placed before i ran the LINQ not after.


I'm using Entity Framework, so I might be wrong. But maybe the Log isn't loaded at all.

Try this:

var options = New DataLoadOptions();
options.LoadWith<Users>(x => x.Log);
db.LoadOptions = options;

// then your code:
User logid = db.Users.Single(p => p.UserID == loginID);
logid.Log.LastLogedin= DateTime.UtcNow;

....
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜