Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 外键具有相同值ASP.NET mvc时如何更新数据库表行_Entity Framework_Asp.net Mvc 4_Sql Update_Insert Update - Fatal编程技术网

Entity framework 外键具有相同值ASP.NET mvc时如何更新数据库表行

Entity framework 外键具有相同值ASP.NET mvc时如何更新数据库表行,entity-framework,asp.net-mvc-4,sql-update,insert-update,Entity Framework,Asp.net Mvc 4,Sql Update,Insert Update,我有一个商店可以填写问卷的应用程序。在这个应用程序中,我有两个表db.StoreAud(pk:auditd),其中包含所有存储信息,以及db.storequests(pk:ReviewId),其中包含所有问题信息 audit是db.storequests表中的外键。现在的问题是,如果某个存储区完成了问卷调查,那么数据会完美地保存在数据库中,但是同一个存储区会再次完成问卷调查。db.storequests会在数据库中创建一个具有新主键值的新行,而不是更新前一行。问题是,如果同一家商店再次进行相同的

我有一个商店可以填写问卷的应用程序。在这个应用程序中,我有两个表db.StoreAud(pk:auditd),其中包含所有存储信息,以及db.storequests(pk:ReviewId),其中包含所有问题信息

audit是db.storequests表中的外键。现在的问题是,如果某个存储区完成了问卷调查,那么数据会完美地保存在数据库中,但是同一个存储区会再次完成问卷调查。db.storequests会在数据库中创建一个具有新主键值的新行,而不是更新前一行。问题是,如果同一家商店再次进行相同的问卷调查,我如何更新上一行。希望这一切都能实现

db.StoreAUD

db.storequests

控制器


我会将您的更新逻辑分为一个新的
update
操作,如下所示:


与StoreAudit的操作方式相同。首先查找它,如果它存在,则更新返回的行。仅在不存在时添加。使用
Update()
操作进行更新。i@ErikFunkenbusch我想我有,但没有成功,你可以发布一些代码,以防我犯了一些错误,如果你没有mind@mxmissile如果需要,可以发布一些示例代码possible@cedPound-你觉得你是怎么做到的?您每次都会将提交的StoreQuestions添加到您的
db.StoreQuestions
,不管怎样。你根本不查商店的问题。我仍然需要更新对象,因为我仍然不知道怎么做
[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; }
[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; }
   [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);
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Update(StoreQuestions storequestions) 
    {
         if (ModelState.IsValid)
         {
              StoreAudit findingAudit = db.StoreAudit.Find(storequestions.AuditId);
              findingAudit.ReadOnlyTickBox = true;

              // update objects here

              db.SaveChanges();
              return RedirectToAction("Details", "Audit", new { id = storequestions.AuditId });
              }

            return View(storequestions);
         }
     }