微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 实体框架更新实体以及子实体(必要时添加/更新)

我的EF上下文中的问题和范围之间存在多对多的关系.在ASP.NET MVC中,我提出一个允许用户编辑特定问题的编辑表单.在表单的底部,是一个允许他们选择适用于此问题的范围的复选框列表.编辑问题时,可能会始终有一些与之相关联的范围 – 这些框将被检查.但是,用户有机会检查更多的范围或删除当前检查的一些范围.我的代码看起来像这样只能保存问题:
using (var edmx = new MayflyEntities())
            {
                Issue issue = new Issue { IssueID = id,TSColumn = formIssue.TSColumn };
                edmx.Issues.Attach(issue);

                UpdateModel(issue);

                if (ModelState.IsValid)
                {
                    //if (edmx.SaveChanges() != 1) throw new Exception("UnkNown error. Please try again.");
                    edmx.SaveChanges();
                    TempData["message"] = string.Format("Issue #{0} successfully modified.",id);
                }
            }

所以,当我尝试添加逻辑来保存关联的范围时,我尝试了几件事情,但最终,这对我来说是最有意义的:

using (var edmx = new MayflyEntities())
            {
                Issue issue = new Issue { IssueID = id,TSColumn = formIssue.TSColumn };
                edmx.Issues.Attach(issue);

                UpdateModel(issue);

                foreach (int scopeID in formIssue.ScopeIDs)
                {
                    var thisScope = new Scope { ID = scopeID };
                    edmx.Scopes.Attach(thisScope);
                    thisScope.ProjectID = formIssue.ProjectID;
                    if (issue.Scopes.Contains(thisScope))
                    {
                        issue.Scopes.Attach(thisScope); //the scope already exists
                    }
                    else
                    {
                        issue.Scopes.Add(thisScope); // the scope needs to be added
                    }
                }

                if (ModelState.IsValid)
                {
                    //if (edmx.SaveChanges() != 1) throw new Exception("UnkNown error. Please try again.");
                    edmx.SaveChanges();
                    TempData["message"] = string.Format("Issue #{0} successfully modified.",id);
                }
            }

但是,不幸的是,只是抛出了以下异常:

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

我究竟做错了什么?

解决方法

存根通常仅对1- *关系有效.
* – *关系引入了一系列不同的挑战.

也就是说,当你连接两端 – 不像1- * – 你仍然不知道关系是否已经存在.

所以这意味着这段代码

if (issue.Scopes.Contains(thisScope))

每次都可能会返回错误.

我会做的是这样的:

edmx.Issues.Attach(issue);
UpdateModel(issue);
// or ctx.LoadProperty(issue,"Scopes") if it is a POCO class.
issue.Scopes.Load(); // hit the database to load the current state.

现在你需要找出你需要添加的从问题中删除.
您可以通过基于ID的比较来做到这一点.

即如果您有一组要与问题相关的范围ID(relevantScopes)

然后这个代码解决了要添加内容以及要删除内容.

int[] toAdd = relatedScopes.Except(issue.Scopes.Select(s => s.ID)).ToArray();
int[] toRemove = issue.Scopes.Select(s => s.ID).Except(relatedScopes).ToArray();

现在要添加你这样做:

foreach(int id in toAdd)
{
   var scope = new Scope{Id = id};
   edmx.Scopes.Attach(scope);
   issue.Scopes.Add(scope);
}

对于每个范围,您需要删除

foreach(int id in toRemove)
{
   issue.Scopes.Remove(issue.Scopes.Single(s => s.ID == id));
}

现在应该形成正确的关系.

希望这可以帮助

Alex

微软

原文地址:https://www.jb51.cc/aspnet/250944.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐