asp.net mvc - SaveChanges not setting value, selecting existing value instead -
when trying commit modified entry database, entity framework selecting values 2 properties opposed setting them.
the code commit entity database follows:
[httppost] [validateantiforgerytoken] public actionresult edit(till till) { if (modelstate.isvalid) { string username = user.identity.name.substring(user.identity.name.lastindexof('\\') + 1); till.modifieddate = datetime.now; till.modifiedbyuid_fk = db.users.where(m => m.name.tolower() == username.tolower()).first().useruid_pk; db.entry(till).state = entitystate.modified; db.savechanges(); return redirecttoaction("index/" + (int)session["siteid"]); } viewbag.siteuid_fk = new selectlist(db.sites, "siteuid_pk", "name", till.siteuid_fk); viewbag.modifiedbyuid_fk = new selectlist(db.users, "useruid_pk", "ehlogin", till.modifiedbyuid_fk); viewbag.createdbyuid_fk = new selectlist(db.users, "useruid_pk", "ehlogin", till.createdbyuid_fk); return view(till); }
when viewed in profiler, looks this:
exec sp_executesql n'update [dbo].[till] set [networkid] = @0, [mid] = @1, [siteuid_fk] = @2, [location] = null, [mastertillno] = @3, [ismaster] = @4, [isbackoffice] = @5, [iscurrent] = @6, [modifiedbyuid_fk] = @7, [createdbyuid_fk] = @8, [processfiles] = @9 ([tilluid_pk] = @10) select [modifieddate], [createddate] [dbo].[till] @@rowcount > 0 , [tilluid_pk] = @10',n'@0 varchar(50),@1 int,@2 int,@3 int,@4 bit,@5 bit,@6 bit,@7 int,@8 int,@9 bit,@10 int', @0='',@1='',@2=4,@3=42,@4=1,@5=0,@6=1,@7=4,@8=1,@9=1,@10=699
i'm guessing there's property of field in database ef uses determine functionality can't figure out is. if has information that'd fantastic :)
edit 1
using ibrahim's answer below, results in relatively similar sql:
exec sp_executesql n'declare @p int update [dbo].[till] set @p = 0 ([tilluid_pk] = @0) select [modifieddate], [createddate] [dbo].[till] @@rowcount > 0 , [tilluid_pk] = @0',n'@0 int',@0=687
this typically happens when properties have data annotation
[databasegenerated(databasegeneratedoption.computed)]
or fluent mapping
.hasdatabasegeneratedoption(databasegeneratedoption.computed)
or storegeneratedpattern
computed
in edmx file.
i'd guess there trigger or default value on modifieddate
, createddate
on columns in database. maybe it's useless set them in application code.
Comments
Post a Comment