entity framework - how to make current primary key update itself instead of table generating a new primary key Asp.net mvc -
please bare me while explain issue having.
i have application stores can complete questionnaire.
within application have 2 tables
db.storeaud(pk:auditid) contains stores information
db.storequests(pk:reviewid) holds questions information. auditid foreign key in db.storequests table.
now here issue if store complete questionnaire data saves in database, same store questionnaire again db.storequests creates new row in database new primary key value instead of updating previous row.
please check following image more information,
i highlighted row updated second row data.
question how can update previous row if same store same questionnaire again. hope made since.
db.storeaud
[databasegenerated(databasegeneratedoption.identity)] [key] [column(order = 1)] public int auditid { get; set; } public string date { get; set; } public int storenumber { get; set; } public string storename { get; set; }
db.storequests
[databasegenerated(databasegeneratedoption.identity)] [key] public int reviewid { get; set; } public int auditid { get; set; } public int questionone { get; set; } public string questiontwo { get; set; } public string questionthree { get; set; } public string questionfour { get; set; }
controller
[httppost] [validateantiforgerytoken] public actionresult create(storequestions storequestions) { if (modelstate.isvalid) { storeaudit findingaudit = db.storeaudit.find(storequestions.auditid); // grabbing id th store audit table findingaudit.readonlytickbox = true; db.storequestions.add(storequestions); db.savechanges(); return redirecttoaction("details", "audit", new { id = storequestions.auditid }); } return view(storequestions); }
you need set entity state modified instead of doing add.
please find sample example below
storeaudit findingaudit = db.storeaudit.find(storequestions.auditid); findingaudit.readonlytickbox = true; db.entry(findingaudit ).state = entitystate.modified; db.savechanges();
Comments
Post a Comment