自跟踪实体 - 添加到集合时尝试插入未更改的实体

发布于 2024-10-22 07:07:44 字数 1276 浏览 1 评论 0原文

使用 EF4 自我跟踪实体。

我有一个“用户”实体,其中包含用户可以属于的“组”集合。我想向该用户添加/删除一些“组”,仅给出组 ID 列表。

    public void UserAddGroups(int userID, List<int> groups)
    {
        var user = Context.Users.Include("Groups").FirstOrDefault(u => u.ID == userID);
        if (user != null)
        {
            // Iterate through the groups that the user already belongs to.
            foreach (var group in user.Groups.ToList())
            {
                // Remove any groups from the user if the new list does not have it.
                if (!groups.Contains(group.ID)) user.Groups.Remove(group);
                // Else remove it from the list of new groups to avoid duplication.
                else groups.Remove(group.ID);
            }
            // Iterate through the group list and add it to the user's list
            // (only a stubby is created)
            foreach (var group in groups) user.Groups.Add(new Group { ID = group }.MarkAsUnchanged());
            Context.Users.ApplyChanges(user);
            Context.SaveChanges();
        }
    }

此方法的结果会在 Context.SaveChanges() 处引发错误。该错误报告“Group”实体不允许 Name 属性为 null

如果我插入新组,这是预期的,但这显然不是我想要做的。我该如何解决这个问题?

Using EF4 Self-tracking entities.

I have a "User" entity that has a collection of "Groups" the user can belong to. I want to add/remove some "Groups" to this user given just a list of Group IDs.

    public void UserAddGroups(int userID, List<int> groups)
    {
        var user = Context.Users.Include("Groups").FirstOrDefault(u => u.ID == userID);
        if (user != null)
        {
            // Iterate through the groups that the user already belongs to.
            foreach (var group in user.Groups.ToList())
            {
                // Remove any groups from the user if the new list does not have it.
                if (!groups.Contains(group.ID)) user.Groups.Remove(group);
                // Else remove it from the list of new groups to avoid duplication.
                else groups.Remove(group.ID);
            }
            // Iterate through the group list and add it to the user's list
            // (only a stubby is created)
            foreach (var group in groups) user.Groups.Add(new Group { ID = group }.MarkAsUnchanged());
            Context.Users.ApplyChanges(user);
            Context.SaveChanges();
        }
    }

The result in this method throws an error at Context.SaveChanges(). The error reports that "Group" entities does not allow null for Name property.

This is expected if I were INSERTING new groups, but thats obviously not what I'm trying to do. How can I fix this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

殤城〤 2024-10-29 07:07:44

你实际上正在插入。通过创建一个新组并将其添加到本质上的集合中,您可以说,这是一个新组,现在添加它。您需要首先从数据库加载该组才能激活对其的跟踪。然后将该组添加到用户组集合中。

amit_g 的解决方案可以工作,但会导致多次数据库调用(每组一个数据库调用)。我会预先加载所有组

You actually ARE inserting. By creating a new group and adding it to the collection in essense you are saying, here is a new group now add it. You need to load the group from the database first to activate tracking on it. then add the group to the users group collection.

amit_g's solution will work but will result in several DB calls ( A DB call per group). I would pre load all the groups up front

骑趴 2024-10-29 07:07:44

尝试一下

user.Groups.Add(Context.Groups.FirstOrDefault(g => g.ID == group));

Try it with

user.Groups.Add(Context.Groups.FirstOrDefault(g => g.ID == group));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文