0

我可能犯了一个愚蠢的错误,但我不知道为什么会这样。我最初保存个人信息,这会在问题表中创建一行,但是当我去更新问题时,它会在问题表中创建一个新行并将 questionId 列更新为添加的新行。代码如下。

public class PersonalInfo
{
    public virtual int PersonalInfoId { get; set; }

    public virtual string FirstName { get; set; }

    public virtual string LastName { get; set; }

    public virtual string Email { get; set; }

    public virtual string CompanyName { get; set; }

    public virtual string Gender { get; set; }

    public virtual DateTime GenerationDate { get; set; }

    [ForeignKey("Question")]
    public int QuestionId { get; set; }

    [ForeignKey("QuestionId")]
    public virtual Question Question { get; set; }

    public PersonalInfo()
    {
        if (Question == null)
            Question = new Question();
    }
}

public class Question
{
    public virtual int QuestionId { get; set; }
    public virtual string Question1 { get; set; }
    public virtual string Question2 { get; set; }
    public virtual string Question3 { get; set; }
    public virtual string Question4 { get; set; }
    public virtual string Question5 { get; set; }
    public virtual string Question6 { get; set; }
    public virtual string Question7 { get; set; }
    public virtual string Question8 { get; set; }
    public virtual string Question9 { get; set; }
    public virtual string Question10 { get; set; }
    public virtual string Question11 { get; set; }
    public virtual string Question12 { get; set; }
}

并更新代码...

[HttpPost]
    public ActionResult Questions(FormCollection form)
    {
        int id = GetCookie();

        //var questions = db.Questions.Find(id);
        var personalInfo = db.PersonalInfoes.Include(i => i.Question).SingleOrDefault(p => p.PersonalInfoId == id);

        personalInfo.Question.Question1 = form["question1"];
        personalInfo.Question.Question2 = form["question2"];
        personalInfo.Question.Question3 = form["question3"];
        personalInfo.Question.Question4 = form["question4"];
        personalInfo.Question.Question5 = form["question5"];
        personalInfo.Question.Question6 = form["question6"];
        personalInfo.Question.Question7 = form["question7"];
        personalInfo.Question.Question8 = form["question8"];
        personalInfo.Question.Question9 = form["question9"];
        personalInfo.Question.Question10 = form["question10"];
        personalInfo.Question.Question11 = form["question11"];
        personalInfo.Question.Question12 = form["question12"];

        db.SaveChanges();

        return RedirectToAction("list");   
    }
4

2 回答 2

0

尝试注释掉这段代码:

if (Question == null)
    Question = new Question();

更新

定义 2 个外键也可能存在问题

[ForeignKey("Question")]
public int QuestionId { get; set; }

[ForeignKey("QuestionId")]
public virtual Question Question { get; set; }

如错误所示,包括Questions_QuestionId

如果将其减少到一个条目会发生什么:

[ForeignKey("QuestionId")]
public virtual Question Question { get; set; }
于 2013-02-14T18:49:23.747 回答
0

根据评论,personalInfo.Question.QuestionId为 0 意味着 EF 将查看该问题记录并确定它实际上是一个新记录,并且与前一个记录没有链接。这就是为什么它每次都重新创建行。

您可以执行以下两种解决方案之一:

1) 将 QuestionId 放入您的会话缓存中,然后在控制器中抓取它,使用它来抓取问题,并在那里进行必要的更新。

2)将 QuestionId 作为隐藏字段放入您的视图中,并确保在提交表单时将其传递到控制器中。从那里抓住问题并进行必要的更新。您甚至可以将该字段设置为可选参数,这样当它为 0 时,您就知道需要创建问题。

于 2013-02-14T19:38:36.167 回答