更新一个多一对多的关系 [英] Updating a many-to-many relationship

查看:120
本文介绍了更新一个多一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问的问题在几个不同的方式的几个不同时期,我还没有得到任何回应。我再试一次,因为我觉得我的解决办法是太令人费解,我必须缺少简单的东西做的。

使用EF 4.1,POCO,API的DbContext,AutoMapper和剃刀在MVC 3应用程序。

我有我的两个实体之间的许多一对多的关系:提案和分类|标签。我可以在提案成功映射(Automapper)我ProposalViewModel包括分类|标签的集合。

在我看来,我使用JavaScript来允许用户通过动态创建的元素添加,更新和删除标签,存储所选择的标签的ID各一个。

我可以成功发布我的ViewModel回到我的与它控制器的分类|标签集合填充(虽然只用ID属性为每个CategoryTag)。

当这个视图模型回发到我的控制器,我不知道如何从视图模型获得这些标签和他们在这样一种方式,db.SaveChanges()正常更新数据库添加到我的模型。

的办法,我有过任何成功是断开映射的分类|标签集合(由不同纳米格他们),通过每个标签遍历并手动在我的背景下看它,然后调用。新增()方法。这是马虎的一些原因,导致我相信我做错了。

任何人都可以提供任何方向呢?

更新:

对于任何人谁是有兴趣的,我的功能code:

 昏暗数p作为新的议案
            昏暗TempTag还可作为CategoryTag
            P = AutoMapper.Mapper.Map(中ProposalViewModel,建议稿)(PVM)            db.Proposals.Attach(p)的
            db.Entry(P).Collection(分类|标签)。load()方法            对于每克拉在pvm.Tags
                TempTag还可= db.CategoryTags.Find(ct.Id)                如果TempTag还可为Nothing然后
                    继续
                万一                如果ct.Tag =removeMe然后
                    p.CategoryTags.Remove(TempTag还可)
                    继续
                万一                p.CategoryTags.Add(TempTag还可)
            下一个
            db.Entry(p)的.STATE = EntityState.Modified
            db.SaveChanges()
            返回RedirectToAction(指数)


解决方案

的唯一工作方式是手工做的这一点 - 你可以读<一个href=\"http://stackoverflow.com/questions/3635071/update-relationships-when-saving-changes-of-ef4-poco-objects/3635326#3635326\">full如果你想问题的说明的。该描述与ObjectContext的API,但API的DbContext只是包装患同样的问题(实际上的DbContext API遭受更大的问题在这种情况下因为这样的话,我会跳过带有手动设置关系的解决方案)。

在做空。一旦您发布的数据返回给控制器必须创建新的上下文实例,并且附上您的的议案和realated 分类|标签。但在那之后,你必须告知上下文你做的改变。这意味着你必须说哪个标签已被添加到的建议和背景已被删除。否则,上下文无法处理您的更改,因为它不会做任何自动合并与数据库中的数据。

要解决这个加载电流的议案最简单的方式与相关分类|标签从数据库(=你会附着实例)和合并输入数据到连接的对象图。这意味着你将手动删除,并根据公布值添加标签。

I've asked the question a few different times in a few different ways and I haven't yet gotten any responses. I'm trying again because I feel like my solution is too convoluted and I must be missing something simpler to do.

Using EF 4.1, POCO, DbContext API, AutoMapper, and Razor in an MVC 3 application.

I have a many-to-many relationship between two of my entities: Proposals and CategoryTags. I can successfully map (Automapper) a Proposal to my ProposalViewModel including the collection of CategoryTags.

In my View, I use javascript to allow the user to add, update, and remove tags by dynamically creating elements, each one that stores the ID of the chosen tag.

I can successfully post my ViewModel back to my controller with it's CategoryTags collection populated (although only with the ID property for each CategoryTag).

When that ViewModel is posted back to my controller, I don't know how to get those tags from the ViewModel and add them to my Model in such a way that db.SaveChanges() updates the database properly.

The only way I've had any success is to disconnect the CategoryTags collection in mapping (by namig them differently), iterate through each tag and manually look it up in my context and then call the .add() method. This is sloppy for a number of reasons which leads me to believe I'm doing it wrong.

Can anyone offer any direction at all?

UPDATE:

For anyone who is interested, my functional code:

            Dim p As New Proposal
            Dim tempTag As CategoryTag
            p = AutoMapper.Mapper.Map(Of ProposalViewModel, Proposal)(pvm)

            db.Proposals.Attach(p)
            db.Entry(p).Collection("CategoryTags").Load()

            For Each ct In pvm.Tags
                tempTag = db.CategoryTags.Find(ct.Id)

                If tempTag Is Nothing Then
                    Continue For
                End If

                If ct.Tag = "removeMe" Then
                    p.CategoryTags.Remove(tempTag)
                    Continue For
                End If

                p.CategoryTags.Add(tempTag)
            Next


            db.Entry(p).State = EntityState.Modified
            db.SaveChanges()
            Return RedirectToAction("Index")

解决方案

The only working way is doing this manually - you can read full description of the problem if you want. The description is related to ObjectContext API but DbContext API is just wrapper suffering same issues (actually DbContext API suffers even more issues in this scenario and because of that I will skip solution with manually setting relationships).

In short. Once you post your data back to the controller you must create new context instance and attach your Proposal and realated CategoryTags. But after that you must inform the context about changes you did. It means you must say context which tags have been added to proposal and which have been removed. Otherwise context cannot process your changes because it doesn't do any automatic merge with data in database.

The easiest way to solve this is loading current Proposal with related CategoryTags from database (= you will have attached instances) and merge incoming data into attached object graph. It means you will manually remove and add tags based on posted values.

这篇关于更新一个多一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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