如何使当前主键自身更新,而不是生成新主键的表Asp.net mvc
我有一个商店可以填写问卷的应用程序 在这个应用程序中,我有两个表如何使当前主键自身更新,而不是生成新主键的表Asp.net mvc,asp.net,entity-framework,asp.net-mvc-4,model-view-controller,insert-update,Asp.net,Entity Framework,Asp.net Mvc 4,Model View Controller,Insert Update,我有一个商店可以填写问卷的应用程序 在这个应用程序中,我有两个表 db.StoreAud(主键:auditd),其中包含所有门店信息 db.storequests(pk:ReviewId),它保存所有问题信息。 Audid是db.storequests表中的外键。 现在的问题是,如果某个存储区完成了问卷调查,那么数据会完美地保存在数据库中,但是同一个存储区会再次完成问卷调查。db.storequests会在数据库中创建一个具有新主键值的新行,而不是更新前一行 请查看下图以了解更多信息, 我希望
db.StoreAud(主键:auditd),其中包含所有门店信息
db.storequests(pk:ReviewId),它保存所有问题信息。
Audid是db.storequests表中的外键。
您需要将实体状态设置为“已修改”,而不是执行“添加” 请在下面找到示例
StoreAudit findingAudit = db.StoreAudit.Find(storequestions.AuditId);
findingAudit.ReadOnlyTickBox = true;
db.Entry(findingAudit ).State = EntityState.Modified;
db.SaveChanges();
您需要将实体状态设置为“已修改”,而不是执行“添加” 请在下面找到示例
StoreAudit findingAudit = db.StoreAudit.Find(storequestions.AuditId);
findingAudit.ReadOnlyTickBox = true;
db.Entry(findingAudit ).State = EntityState.Modified;
db.SaveChanges();
通过获取add方法,没有任何内容被更新,甚至没有添加到数据库中,也没有任何内容是storequestions pk,我正在尝试更新它,而不是storeaudit表,如果你明白我的意思的话。通过获取add方法,没有任何内容被更新,甚至没有内容被添加到数据库中,也没有内容是storequestions pk,我正在尝试更新它而不是storeaudit表如果你明白我的意思的话
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(StoreQuestions storequestions)
{
if (ModelState.IsValid)
{
StoreAudit findingAudit = db.StoreAudit.Find(storequestions.AuditId); // grabbing the id from th store audit table
findingAudit.ReadOnlyTickBox = true;
db.StoreQuestions.Add(storequestions);
db.SaveChanges();
return RedirectToAction("Details", "Audit", new { id = storequestions.AuditId });
}
return View(storequestions);
}
StoreAudit findingAudit = db.StoreAudit.Find(storequestions.AuditId);
findingAudit.ReadOnlyTickBox = true;
db.Entry(findingAudit ).State = EntityState.Modified;
db.SaveChanges();