C# Entity Model won't correctly save changes to SQL database -
here's brief summary of code - written long time ago , there on 50 properties written way, yet when added new one, both in model , within viewmodel, refuses save changes:
from view have:
@html.editorfor(m => m.phonenumber )
from viewmodel have:
public string registration {get;set;}
from controller have:
public async task<actionresult> savevehicleinfo(vehicleviewmodel vm){ vehicle vehicle = mapper.map<vehicleviewmodel, vehicle>(vm); (automapper library) var changes = false; vehicle dbvehicle = await context.vehicles.findasync(vehicle.id) //a load more properties etc string phonenumberchange = ""; if (dbvehicle.phonenumber != vehicle.phonenumber) { phonenumberchange = " - phone number changed " + vehicle.phonenumber + " " + dbvehicle.phonenumber; dbvehicle.phonenumber = vehicle.phonenumber; changes = true; } //at end: _db.vehicles.attach(dbvehicle); _db.entry(dbvehicle).state = entitystate.modified; _db.notes.add(note); try { await _db.savechangesasync(); } catch (exception e) { var str = ""; } return #stuff#; }
i know not best way of doing it, said written long time ago.
on inspecting item dbvehicle in line:
_db.entry(dbvehicle).state = entitystate.modified;
it correctly shows dbvehicle.phonenumber being new value. shows this:
phonenumberchange = " - phone number changed newvalue oldvalue"
the save statement goes through, sql logs not show phonenumber being included in update statement. other values save correctly, except new 1 i've done. can manually enter in database, savechangesasync method fires without throwing anything, cannot figure out why it's doing this.
have cleaned/rebuilt whole solution. no idea what's going on.
entity framework version 6.
i think perhaps going wrong in entity framework somewhere. after lot of messing around, deleting model , re-adding seemed fix problem.
Comments
Post a Comment