Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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
C# 附加到DB中的IList会在MVC中使用EF引发引用完整性约束冲突_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 附加到DB中的IList会在MVC中使用EF引发引用完整性约束冲突

C# 附加到DB中的IList会在MVC中使用EF引发引用完整性约束冲突,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我将POCO类设置为: public class Survey { public int ID { get; set; } public string Name { get; set; } public virtual IList<Image> Images { get; set; } } public class Image { [Key] public int ImageID { get; set; } public int Su

我将POCO类设置为:

public class Survey
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual IList<Image> Images { get; set; }
}

public class Image
{
    [Key]
    public int ImageID { get; set; }
    public int SurveyID { get; set; }
    public string ImageUrl { get; set; }
    public virtual Survey Survey { get; set; }
}
我已检查图像[0和1].ImageId、SurveyID和ImageUrl是否已正确绑定。Image[2]、ImageId和SurveyID设置为0,但我希望EF在将其保存到DB上时设置它们

我使用modelbuilder对这些选项进行了尝试,但有一些地方我做得不对。谢谢你的帮助

这是我得到的错误: 其他信息:发生引用完整性约束冲突:关系一端的“Survey.ID”属性值与“Image.SurveyID”属性值不匹配

编辑编辑附加代码

    public ActionResult Edit([Bind(Include = "ID,Name, Images")] Survey survey)
    {
        if (ModelState.IsValid)
        {
            db.Entry(survey).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(survey);
    }

我可以通过在我的edit actionresult中单独添加图像来解决此问题

    public ActionResult Edit([Bind(Include = "ID,Name,Images")] Survey survey)
    {
        if (ModelState.IsValid)
        {
            for (var i = 0; i < survey.Images.Count; i++ )
            {
                if ((survey.Images[i].SurveyID == 0) && (survey.Images[i].ImageID == 0))
                {
                    survey.Images[i].SurveyID = survey.ID;
                    db.Images.Add(survey.Images[i]);
                }
            }

            db.Entry(survey).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(survey);
    }
public ActionResult编辑([Bind(Include=“ID,Name,Images”)]调查)
{
if(ModelState.IsValid)
{
对于(var i=0;i
您可以发布您的编辑操作代码。添加了“编辑”操作结果您在尝试编辑调查或图像时出错了吗?问题不清楚我想你是在试图编辑调查POCO。是否将表单中的surveyID绑定回编辑方法?是,已绑定。我正在尝试附加到IList图像。
    public ActionResult Edit([Bind(Include = "ID,Name,Images")] Survey survey)
    {
        if (ModelState.IsValid)
        {
            for (var i = 0; i < survey.Images.Count; i++ )
            {
                if ((survey.Images[i].SurveyID == 0) && (survey.Images[i].ImageID == 0))
                {
                    survey.Images[i].SurveyID = survey.ID;
                    db.Images.Add(survey.Images[i]);
                }
            }

            db.Entry(survey).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(survey);
    }