首页 新闻 会员 周边

关于EntityFramework 多对多 更新的问!

0
悬赏园豆:20 [已关闭问题] 关闭于 2014-11-21 21:26
/// <summary>
        /// 处理简单的聚合更新操作。
        /// </summary>
        /// <typeparam name="TDataList">包含数据传输对象的列表类型。</typeparam>
        /// <typeparam name="TAggregateRoot">聚合根类型。</typeparam>
        /// <param name="dataList">包含了一系列数据传输对象的列表实例。</param>
        /// <param name="repository">用于特定聚合根类型的仓储实例。</param>
        /// <param name="idFieldFunc">用于获取数据传输对象唯一标识值的回调函数。</param>
        /// <param name="fieldUpdateAction">用于执行聚合更新的回调函数。</param>
        /// <returns>包含了已更新的聚合的数据的列表。</returns>
        protected TDataList PerformUpdateObjects<TDataList, TAggregateRoot>(TDataList dataList,
            IRepository<TAggregateRoot> repository,
            Func<TAggregateRoot, string> idFieldFunc,
            Action<TAggregateRoot, TAggregateRoot> fieldUpdateAction)
            where TDataList : List<TAggregateRoot>, new()
            where TAggregateRoot : class, IAggregateRoot
        {
            if (dataList == null)
                throw new ArgumentNullException("dataList");
            if (repository == null)
                throw new ArgumentNullException("repository");
            if (idFieldFunc == null)
                throw new ArgumentNullException("idFieldFunc");
            if (fieldUpdateAction == null)
                throw new ArgumentNullException("fieldUpdateAction");
            TDataList result = null;
            if (dataList.Count > 0)
            {
                result = new TDataList();
                foreach (var model in dataList)
                {
                    if (IsEmptyGuidString(idFieldFunc(model)))
                        throw new ArgumentNullException("idFieldFunc");
                    var id = new Guid(idFieldFunc(model));
                    var ar = repository.GetByKey(id);
                    fieldUpdateAction(ar, model);
                    repository.Update(ar);
                    result.Add(ar);
                }
                repository.Context.Commit();
            }
            return result;
        }

这是网上有家写的代码,但是在做数据更新的时候只要是一对多或都多对多,都没有办法更新?

问题补充:

怎么没有回答哦,EF在分层架构中的多对多数据修改是怎么实现的啊?

The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.

 

难道只能用单例模式

浪潮之巅的主页 浪潮之巅 | 初学一级 | 园豆:83
提问于:2014-07-23 16:30
< >
分享
所有回答(1)
0

不知道你的代码哪里来的,也没细看,是否能给出来源参考下?

 

关于EF的多对多,需要使用Map功能,Map一个表,配置多对多是很简单的(我指的是codefirst)。

519740105 | 园豆:5810 (大侠五级) | 2014-08-22 11:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册