【问题标题】:DbUpdateExecption- An error occurred while saving entities that do not expose foreign key properties for their relationshipsDbUpdateExecption - 保存不为其关系公开外键属性的实体时发生错误
【发布时间】:2013-06-05 12:00:54
【问题描述】:

我的 Asp.net Mvc4 应用程序出现问题。

我设计了一个“用户”表和一个“可投影”表,并且是多对多的关系。

我的用户表:

 public partial class User
{
    public User()
    {
        this.Role = new HashSet<Role>();
        this.Project = new HashSet<Project>();
    }

    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public virtual ICollection<Role> Role { get; set; }
    public virtual ICollection<Project> Project { get; set; }
}

还有我的项目表:

public partial class Project
{
    public Project()
    {
        this.Thresholds = new HashSet<Thresholds>();
        this.User = new HashSet<User>();
        this.Testrelease = new HashSet<Testrelease>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Thresholds> Thresholds { get; set; }
    public virtual ICollection<User> User { get; set; }
    public virtual ICollection<Testrelease> Testrelease { get; set; }
}

在我的项目控制器中:

public ActionResult EditProject(int id = 0)
    {
        Project project = db.Project.Find(id);
        if (project == null)
        {
            return HttpNotFound();
        }
        List<CheckBoxListInfoInt> userCheck = new List<CheckBoxListInfoInt>();
        foreach (User U in db.User.ToList())
        {
            userCheck.Add(new CheckBoxListInfoInt
            {
                ValueInt = U.Id,
                DisplayText = U.Username,
                IsChecked = (project.User.Contains(U))
            });
        }
        ViewBag.P_usrCB = userCheck;
        ViewData["pid"] = id;
        return View(project);
    }

    //
    // POST: /KPI_Data/Edit/5

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult EditProject(Project project)
    {
        if (ModelState.IsValid)
        {
            db.Project.Attach(project);

            db.Entry(project).State = EntityState.Modified;

            if (project.User.Count > 0)
                project.User.Clear();


            List<string> usrlist = new List<string>();
            var ucb = Request.Form["P_usrCB"];
            if (ucb != null)
                foreach (string item in ucb.Split(','))
                {
                    int UserId = Convert.ToInt32(item);
                    User usr = db.User.Single(x => x.Id == UserId);
                    project.User.Add(usr);
                    usrlist.Add(usr.Username);
                }
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(project);
    }

错误发生在

db.SaveChanges();

在我的 HttpPost ActionMethod 中

错误信息是:

保存不公开外键的实体时出错 他们关系的属性。 EntityEntries 属性将 返回 null,因为无法将单个实体标识为源 的例外。可以进行保存时的异常处理 通过在实体类型中公开外键属性更容易。看 InnerException 了解详情。

为什么会出现此错误?解决方案是什么?谢谢

【问题讨论】:

  • 检查外键映射的属性

标签: c# asp.net-mvc entity-framework asp.net-mvc-4 entity-framework-5


【解决方案1】:

避免使用db.Entry(ptvm.place).State = EntityState.Modified;,因为它会导致没有更新的并发冲突。将 ViewModel 用于多对多关系而不是两个表。

你必须使用UpdateModel(table object, "model");

完整示例如下:

[HttpPost]
public ActionResult PlaceTag(PlacesWithTagsViewModel model)
{
    if (ModelState.IsValid)
    {
       tag tagtest = GetTagById(model.tagid);
       tag.name= model.tag.name;
       tag.nameplural = model.tag.nameplural;
       UpdateModel(tag, "model");
       db.SaveChanges();
       return RedirectToAction("Index", "Dashboard", new { id = 5 });
    } 
}

UpdateModel 的优点是您只需提及您更新的那些字段,避免那些保持静态的字段。通过这种方式,您可以在 Edit View 中使用 Viewmodel 更新相关数据。

【讨论】:

    猜你喜欢
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 2015-08-01
    • 2019-08-23
    相关资源
    最近更新 更多