c# - MVC 5 Edit Action Saving Input Error -
when i'm editing "salesmember" , i'm trying save new file path / or old file path, based on user input, should either overwrite image, or use previous image stored in db.
but, when try modify entity state, before save changes in sql server, error:
an exception of type 'system.invalidoperationexception' occured in entityframework.dll not handled in user code. additional information: attaching entity of type 'salesmember' failed because entity of same type has same primary key value. can happen when using 'attach' method or setting state of entity 'unchanged' or 'modified' if entities in graph have conflicting key values. may because entities new , have not yet received database-generated key values. in case use 'add' method or 'added' entity state track graph , set state of non-new entities 'unchanged' or 'modified' appropriate.
i'm modifying salesmember primary key. which, saves image locally, won't save db row.
my edit action -----
[httppost] [validateantiforgerytoken] public actionresult edit([bind(include="salesmemberid,firstname,lastname,pagename,cellphone,email,inactiveredirectto,customtext,photo,active, position")] salesmember salesmember, httppostedfilebase file) { salesmember salesphoto = db.salesmembers.find(salesmember.salesmemberid); salesmember.photo = saveimage(file, salesmember, salesphoto); if (modelstate.isvalid) { salesmember.adddate = salesphoto.adddate; salesmember.updatedate = datetime.now; salesmember.ipaddress = request.userhostaddress; salesmember.adduser = "admin"; salesmember.updateuser = "admin"; db.entry(salesmember).state = entitystate.modified; db.savechanges(); return redirecttoaction("index"); } return view(salesmember); }
salesphoto save function --
private string saveimage(httppostedfilebase file, salesmember salesmember, salesmember salesphoto = null) { string photo = ""; string returnphoto = ""; //file blank if( file == null ) //if file path user in db, return file path if(! string.isnullorempty(salesphoto.photo)) return "/images/no-upload-icon.jpg"; //if file ! empty, save image uploaded photo = server.mappath("/images/" + salesmember.pagename.tostring() + ".jpg"); file.saveas(photo); returnphoto = "/images/" + salesmember.pagename.tostring() + ".jpg"; return returnphoto; }
i believe has linq query, when remove linq query & saveimage method , save photo test path(i.e., "/images/foo.jpg"), works.
thanks in advance.
you can not have 2 entities (same type) same primary keys in memory in entity framework. salesmember
, salesphoto
if want have adddate
in form edit, can include hiddenfield
in form, or
my recommendation deal viewmodels not ef models in views ignore these issues
[httppost] [validateantiforgerytoken] public actionresult edit([bind(include="salesmemberid,firstname,lastname,pagename,cellphone,email,inactiveredirectto,customtext,photo,active, position")] salesmember salesmember, httppostedfilebase file) { salesmember salesphoto = db.salesmembers.asnotracking().find(salesmember.salesmemberid); salesmember.photo = saveimage(file, salesmember, salesphoto); if (modelstate.isvalid) { salesmember.adddate = salesphoto.adddate; salesmember.updatedate = datetime.now; salesmember.ipaddress = request.userhostaddress; salesmember.adduser = "admin"; salesmember.updateuser = "admin"; db.entry(salesmember).state = entitystate.modified; db.savechanges(); return redirecttoaction("index"); } return view(salesmember); }
Comments
Post a Comment