保存两个实体的N-N协会之间的关系 [英] Save a relation with between two entities an N-N association

查看:337
本文介绍了保存两个实体的N-N协会之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体框架4.0,与POCO对象。从数据库中生成的EDMX模型文件。

I've a Entity Framework 4.0, with poco object. the edmx model file is generated from the database.

这DataContext的是通过WCF服务访问,它只是意味着我收到一些对象,我需要将其连接到当前。DataContext的(或与键对应关系重新加载)

This datacontext is accessed through WCF service, it's only mean that I receive some objects and I need to attach them to the current datacontext(or reload them with the key correspondance).

一切似乎都做工精细,除了一种情况:

Everything seems to work fine, except for one case:

我有两条表之间的关系NN,所以我的关联表,不超过两个表的ID其他任何领域:

I've a N-N relationship between two table, so I've an association table, without any field other than ID of two tables:

LINQ将其转化成下面的模式,这似乎是正确的。

LINQ transform this into the following schema, this seems to be right.

当我检索数据,是没有问题的,数据我插入自己在Right_group正确地转化为我的收藏权利/组在新的对象。

When I retrieve data, there is no problem, data I've inserted myself in the Right_group are correctly transformed into "new object in my collection of Rights/Groups".

但如果我尝试修改的东西和保存,这是行不通的。

But if I try to modify something and save, it doesn't work

 public void SaveRights(Group group, List<Rights> rights){
     //here, group and rights are objects attached to the database
     group.Rights.Clear();
     group.Rights.AddRange(rights);
     _dataContext.SaveChanges();
 }



所以我的问题是:如何保存这两个对象的关系?
谢谢!

So my question is: How to save this "relationship" of two objects ? Thank you!

推荐答案

如果你想避免加载从数据库中的对象首先,你可以像这样做(从我aplications之一采取代号所以你必须去适应它):

If you want to avoid loading the objects from the database first you can do it like this(Code taken from one of my aplications so you will have to adapt it):

    public void AddAndRemovePersons(int id, int[] toAdd, int[] toDelete)
    {
        var mailList = new MailList { ID = id, ContactInformations = new List<ContactInformation>() };        
        this.db.MailLists.Attach(mailList);

        foreach (var item in toAdd)
        {
            var ci = new ContactInformation { ID = item };
            this.db.ContactInformations.Attach(ci);
            this.db.ObjectStateManager.ChangeRelationshipState(mailList, ci, ml => ml.ContactInformations, System.Data.EntityState.Added);
        }

        foreach (var item in toDelete)
        {

            var ci = new ContactInformation { ID = item };
            this.db.ContactInformations.Attach(ci);
            this.db.ObjectStateManager.ChangeRelationshipState(mailList, ci, ml => ml.ContactInformations, System.Data.EntityState.Deleted);
        }
    }



我发现硬删除关系,创造这么我离开的代码在那里。有关此溶液的一件事是,在此之前的功能正在运行两个邮件列表和联系人存在。我重视他们作出的状态管理器跟踪。

I found deleting the relationship as hard as creating it so I left that code in there. One thing about this solution is that both the maillist and the contacts exist prior to this function being run. I attach them to make the state manager track them.

如果您要增加您也想救你可以使用

If you are adding new objects that you also want to save you would use the

this.db.MailLists.AddObject(在这里你新项目)

我希望帮助!

这篇关于保存两个实体的N-N协会之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆