ASP.NET MVC / EF4 / POCO /存储库 - 如何更新的关系? [英] ASP.NET MVC / EF4 / POCO / Repository - How to Update Relationships?

查看:175
本文介绍了ASP.NET MVC / EF4 / POCO /存储库 - 如何更新的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之间有1 .. *关系的查看建议

I have a 1..* relationship between Review and Recommendations.

我的模型的相关部分(这也是由EF4映射POCO):

The relevant portion of my model (which is also the POCO mapped by EF4):

public class Review
{
   public ICollection<Recommendations> Recommendations { get; set; }
}

在一个修改视图后,我重新present的的建议的为一组复选框。

On an Edit View, i represent the Recommendations as a set of checkboxes.

当我尝试添加新的建议书为编辑审查的一部分(例如检查另一个框),什么都没发生 - 我知道为什么...

When i try and add a new Recommendation as part of editing the Review (e.g check another box), nothing is happening - and i know why...

我使用了存根技术更新我的实体 - 例如我创建了一个实体使用相同的密钥,将其连接到图形,然后执行 ApplyCurrentValues​​ 。但是,这仅适用于标量的属性,而不是为导航属性。

I use the "stub technique" to update my entities - e.g i create a entity with the same key, attach it to the graph, then do ApplyCurrentValues. But this only works for scalar properties, not for navigational properties.

我发现<一个href=\"http://stackoverflow.com/questions/3721755/update-non-scalar-entities-in-entity-framework-v4\">this StackOverflow的问题这看起来不错,但我试图找出如何得到这个与POCO的/存储库(和ASP.NET MVC - 脱离上下文)的工作。

I found this StackOverflow question which looks good, but i am trying to work out how to get this to work with POCO's/Repository (and ASP.NET MVC - detached context).

由于我使用POCO的, review.Recommendations 的ICollection&LT;推荐&GT; ,这样我就可以'做T review.Recommendations.Attach 。我不使用自跟踪实体要么,所以我需要手动图/更改跟踪工作 - 这已经不是一个问题到现在为止

As i'm using POCO's, review.Recommendations is an ICollection<Recommendation>, so i can't do review.Recommendations.Attach. I'm not using Self-Tracking Entities either, so i need to manually work with the graph/change tracking - which hasn't been a problem until now.

所以,你可以想像的场景:

评论:


  • 的建议书(的ICollection&LT;推荐&GT; ):

    • RecommendationOne(建议

    • RecommendationTwo(建议

    • Recommendations (ICollection<Recommendation>):
      • RecommendationOne (Recommendation)
      • RecommendationTwo (Recommendation)

      如果在编辑视图IM,两个复选框都已经检查。第三个(重presenting RecommendationThree)为选中

      If im on the edit view, two of the checkboxes are already checked. The third one (representing RecommendationThree) is unchecked.

      但是,如果我检查中,上述模型变为:

      But if i check that box, the above model becomes:

      评论:


      • 的建议书(的ICollection&LT;推荐&GT; ):

        • RecommendationOne(建议

        • RecommendationTwo(建议

        • RecommendationThree(建议

        • Recommendations (ICollection<Recommendation>):
          • RecommendationOne (Recommendation)
          • RecommendationTwo (Recommendation)
          • RecommendationThree (Recommendation)

          ,所以我需要RecommendationThree附加到图形作为新实体

          And so i need to attach RecommendationThree to the graph as a new entity.

          我是否需要隐藏字段比较已发布数据的现有实体?或者我应该实体存放在TempData的和比较,以张贴的实体?

          Do i need hidden fields to compare the posted data the existing entity? Or should i store the entity in TempData and compare that to the posted entity?

          修改

          要避免混淆,这里是完整的应用程序栈的呼叫:

          To avoid confusion, here is the full app stack call:

          ReviewController

          [HttpPost]
          public ActionResult Edit(Review review)
          {
             _service.Update(review); // UserContentService
             _unitOfWork.Commit();
          }
          

          UserContentService

          public void Update<TPost>(TPost post) where TPost : Post, new()
          {
             _repository.Update(post); // GenericRepository<Post>
          }
          

          GenericRepository - 作为 GenericRepository&LT;邮政和GT;

          GenericRepository - used as GenericRepository<Post>

          public void Update<T2>(T2 entity) where T2 : class, new()
          {
             // create stub entity based on entity key, attach to graph.
          
             // override scalar values
             CurrentContext.ApplyCurrentValues(CurrentEntitySet, entity);
          }
          

          因此​​,更新(或添加删除)库的方法需要调用每一个建议,这取决于它的新/修改/删除。

          So, the Update (or Add or Delete) Repository methods needs to be called for each recommendation, depending it's new/modified/deleted.

          推荐答案

          也许,我需要更多的上下文,但什么错:

          Perhaps I need more context but whats wrong with:

          recommendations.Add(newRecomendation)
          

          在回复评论:

          好了,所以什么错

          SomeServiceOrRepository.AddNewRecommendation( newRecommendation )
          

          SomeServiceOrRepository.AddNewRecommendation( int parentId, newRecommendation )
          

          最后一句话?你指的是两个问题?

          Last Sentence? You mean the two questions?

          这不应该是很难的。

          要总结我的回答,我认为你正在做的事情硬办法,真正应该注重张贴对应于CRUD操作你试图完成表单值。

          To summarize my answer I think you are doing things "the hard way" and really should focus on posting form values that correspond to the CRUD action your trying to accomplish.

          如果一个新的实体可能会在同一时间来作为你的编辑实体,你真的应该preFIX他们不同所以模型绑定可以在其上回升。即使你有多个新项目,你可以使用同一个[0]语法只是preFIX名称字段新建或东西。

          If a new entity could come in at the same time as your edited entities you should really prefix them differently so the model binder can pick up on it. Even if you have multiple new items you can use the same [0] syntax just prefix the "name" field with New or something.

          在这种情况下,你不能依靠实体框架图功能,因为从集合中移除一个实体绝不意味着它应该删除设置很多倍。

          A lot of times in this scenario you can't rely on Entity Frameworks graph features because removing an entity from a collection never means it should be set for deletion.

          如果表单是不可变的,你也可以使用generized附加功能关闭对象集的尝试:

          If the form is immutable you could also try using the generized attach function off of ObjectSet:

          theContect.ObjectSet<Review>().Attach( review )
          

          办法摆脱这种吨。也许你可以发布您的控制器和视图code?

          Tons of ways out of this. Maybe you could post your controller and view code?

          这篇关于ASP.NET MVC / EF4 / POCO /存储库 - 如何更新的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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