1

私は問題を説明しながら私と一緒に裸にしてください。新しいプライマリキーを生成するテーブルの代わりに、現在のプライマリキーを更新する方法。Asp.net mvc

店舗でアンケートを完了できるアプリケーションがあります。私は今ここに二つのテーブル

  1. db.StoreAud(pk:AuditId) which contains all the stores information
  2. db.storequests(pk:ReviewId) which holds the all questions information. AuditId is a foreign key in db.storequests table.

を持ってこのアプリケーション内

は、ストアがデータがデータベースに完全に保存したアンケートを完了した場合に問題であるしかしあります同じストアがアンケートを再度実行すると、db.storequestsは前の行を更新するのではなく、新しい主キー値を使用してデータベースに新しい行を作成します。

は、2行目のデータを更新することが、私はハイライトされた行をしたいと思い Image

、詳細については、以下の画像をご確認ください。

質問同じ店舗で同じアンケートをもう一度やり取りすると、以前の行を更新するにはどうすればよいですか。それ以来これが欲しい。

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; } 

コントローラ

[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); 
    } 

答えて

0

addを実行する代わりにエンティティの状態を変更する必要があります。

addメソッドは何も更新されなかったり、データベースに追加しても、そのstorequestionsは、私はあなたの場合は代わりにstoreauditテーブルの更新しようとしているPKされているの取得により

StoreAudit findingAudit = db.StoreAudit.Find(storequestions.AuditId); 
findingAudit.ReadOnlyTickBox = true; 
db.Entry(findingAudit).State = EntityState.Modified; 
db.SaveChanges(); 
+0

以下のサンプル例を見つけてください。私が何を意味するか見る – cedPound

関連する問題