实体框架:由于跟踪而无法保存数据

时间:2019-02-10 17:24:43

标签: c# entity-framework entity-framework-core asp.net-core-2.0

我上了一堂课。反馈和步骤。我正在使用SQL数据库和.Net Core2。我可以保存主要属性,但不能保存子类FeedbackSteps属性

public class FeedbackModel
{
    [Key]
    public int FeedBackID { get; set; }
    public DateTime FBDate { get; set; }
    public bool? VideoStatus { get; set; }
    public string VideoDetail { get; set; }
    public string PITFeedBack { get; set; }
    public int ActivityID { get; set; }
    public virtual ActivityModel Activity { get; set; }
    public int ClientID { get; set; }
    public virtual ClientModel Client { get; set; }
    public int? SupportPlanID { get; set; }
    public virtual SupportPlanModel SupportPlan { get; set; }
    public int EmployeeID { get; set; }
    public virtual Employee Employee { get; set; }
    public bool FeedbackStatus { get; set; } = true;

    virtual public List<FeedbackStepModel> FeedbackSteps { get; set; }
}

public class FeedbackStepModel
{
    [Key]
    public int FeedbackStepID { get; set; }
    public int FeedbackID { get; set; } = 0;
    public int SupportPlanID { get; set; }    
    public int StepNumber { get; set; }
    public string StepDetail { get; set; }
    public string AchivmentStatus { get; set; }
    public string AchivmentComment { get; set; }

}

这是发布方法。查看返回值编辑或更新的反馈,我只想用新数据更新数据库

    [HttpPost]
    public IActionResult Edit(FeedbackModel feedback)
    {
        if (ModelState.IsValid)
        {
            feedbackRepository.Save(feedback);
            TempData["message"] = $"Feedback has been saved";
            return RedirectToAction("Index");
        }
    }

编辑后,我想保存它...

    public void Save(FeedbackModel feedback)
    {
        if (feedback.FeedBackID == 0)
        {
            context.FeedbackModels.Add(feedback);
        }
        else
        {
            FeedbackModel dbEntry = context.FeedbackModels.Include(s => s.FeedbackSteps).FirstOrDefault(a => a.FeedBackID == feedback.FeedBackID);

            if (dbEntry != null)
            {
                dbEntry.FeedBackID = feedback.FeedBackID;
                dbEntry.FBDate = feedback.FBDate;
                dbEntry.VideoStatus = feedback.VideoStatus;
                dbEntry.VideoDetail = feedback.VideoDetail;
                dbEntry.SupportPlanID = feedback.SupportPlanID;
                dbEntry.ActivityID = feedback.ActivityID;
                dbEntry.PITFeedBack = feedback.PITFeedBack;
                dbEntry.ClientID = feedback.ClientID;
                dbEntry.EmployeeID = feedback.EmployeeID;                   
                dbEntry.FeedbackStatus = feedback.FeedbackStatus;
                dbEntry.FeedbackSteps = feedback.FeedbackSteps;
            }
        }
        context.SaveChanges();
    }

但是它总是给我这个错误

Message=The instance of entity type 'FeedbackStepModel' cannot be tracked because another instance with the key value '{FeedbackStepID: 1}' is already being tracked. 
When attaching existing entities, ensure that only one entity instance with a given key value is attached.

1 个答案:

答案 0 :(得分:1)

您对带有子级(FeedbackModel)的FeedbackSteps更新操作应如下:

FeedbackModel dbEntry = context.FeedbackModels.Include(s => s.FeedbackSteps).FirstOrDefault(a => a.FeedBackID == feedback.FeedBackID);

if (dbEntry != null)
{
      dbEntry.FeedBackID = feedback.FeedBackID;
      dbEntry.FBDate = feedback.FBDate;
      dbEntry.VideoStatus = feedback.VideoStatus;
      dbEntry.VideoDetail = feedback.VideoDetail;
      dbEntry.SupportPlanID = feedback.SupportPlanID;
      dbEntry.ActivityID = feedback.ActivityID;
      dbEntry.PITFeedBack = feedback.PITFeedBack;
      dbEntry.ClientID = feedback.ClientID;
      dbEntry.EmployeeID = feedback.EmployeeID;                   
      dbEntry.FeedbackStatus = feedback.FeedbackStatus;

      dbEntry.FeedbackSteps.Clear(); // First you have to clear the existing feedBackSteps

      foreach(FeedbackStep feedBackStep in feedback.FeedbackSteps)
      {
         dbEntry.FeedbackSteps.Add(feedBackStep); // You have to add new and updated feedBackStep here.
      }
}

如果dbEntry.FeedbackSteps.Clear();不起作用(可能在EF Core 2.0或更低版本中,Clear()不起作用),则用以下代码替换dbEntry.FeedbackSteps.Clear();

foreach(FeedbackStep feedbackStepToBeRemoved in dbEntry.FeedbackSteps)
{
   context.Remove(feedbackStepToBeRemoved);
}