Automapper循环引用导致更新错误

时间:2013-12-02 10:08:56

标签: c# asp.net-mvc automapper

编辑:我也忘了提到Flat模型的数据最终将从单独的数据库中提取而不是手​​动创建。

批量更新从更新返回了意外的行数;实际行数:0;预期:1

是我在两个模型上使用Automapper时遇到的错误,它们互相循环引用。我有一个订单,其中包含订单行列表,所有订单行都会参考订单。但是,我也从平面模型映射到每个类。所以模型的总数是:

  • FlatPickingAssignment
  • FlatPickingAssignmentItem
  • PickingAssignment
  • PickingAssignmentItem

总而言之,我将平面模型映射到普通模型(具有循​​环参考),然后保存拣配分配。

            FlatPickingAssignment pickingAssignment = new FlatPickingAssignment{
               HostId = "1002",
               AssignmentItems = assignmentItems,
               Description = "SecondAssignment",
               IsChase = false,
               GoalTime = 10.0,
               SummaryPromptType = 0,
               OverridePrompt = "",
               LocationCode = "LOC001",
               DirectLoad = 0,
               AllowDeliveryLocationOverride = 0,
               VehicleLicence = "",
               CustomerCode = "Cust002",
               TotalWeight = 0.0,
               TotalItems = 0,
               SingleSKU = false,
               SingleBatch = false,
               CreatedDate = System.DateTime.Now,
               StartDate = System.DateTime.Now,
               EndDate = System.DateTime.Now
            };

            FlatPickingAssignmentItem t = new FlatPickingAssignmentItem
            {
                Id = 1,
                Assignment = pickingAssignment,
                ItemStatusId = 10,
                ItemTypeId = 10,
                LocationCode = "LOC002",
                PickedDate = System.DateTime.Now,
                CurrentUserOperatorId = "David",
                AssignedUserOperatorId = "Michael",
                ProductCode = "PROD001",
                PickingContainerId = 2,
                LotId = 1
            };

            assignmentItems.Add(t);
            pickingAssignment.AssignmentItems = assignmentItems;

            PickingAssignment translatedAssignment = translate(pickingAssignment);
            SavePickingAssignment(translatedAssignment);


private PickingAssignment translate(FlatPickingAssignment flatPickingAssignment)
        {
            try
            {
                Mapper.CreateMap<FlatPickingAssignmentItem, PickingAssignmentItem>()
                   .ForMember(dest => dest.Status, opt => opt.ResolveUsing<PAItemStatusResolver>())
                   .ForMember(dest => dest.Type, opt => opt.ResolveUsing<PAItemTypeResolver>())
                   .ForMember(dest => dest.Location, opt => opt.ResolveUsing<AssignmentItemLocationResolver>())
                   .ForMember(dest => dest.Product, opt => opt.ResolveUsing<ProductResolver>())
                   .ForMember(dest => dest.PickedContainer, opt => opt.ResolveUsing<PickingContainerResolver>())
                   .ForMember(dest => dest.Lot, opt => opt.ResolveUsing<LotsResolver>())
                   .ForMember(dest => dest.AssignedUser, opt => opt.ResolveUsing<AssignedUserResolver>())
                   .ForMember(dest => dest.CurrentUser, opt => opt.ResolveUsing<CurrentUserResolver>())
                   .ForMember(dest => dest.Assignment, opt => opt.Ignore());

                Mapper.CreateMap<FlatPickingAssignment, PickingAssignment>()
                    .ForMember(dest => dest.Customer, opt => opt.ResolveUsing<CustomerResolver>())
                    .ForMember(dest => dest.DeliveryLocation, opt => opt.ResolveUsing<AssignmentLocationResolver>())
                    .AfterMap((src, dest) =>
                    {
                        foreach (var i in dest.AssignmentItems)
                            i.Assignment = dest;
                    });

                PickingAssignment pickingAssignment = Mapper.Map<FlatPickingAssignment, PickingAssignment>(flatPickingAssignment);

                return pickingAssignment;
            }
            catch
            {
                // add in later 
                return null;
            }


        }


 private void SavePickingAssignment(PickingAssignment assignment)
        {
            try {
                    using (ISession session = NHibernateHelper.OpenSession())
                    {
                        using (ITransaction t = session.BeginTransaction())
                        {
                            session.SaveOrUpdate(assignment);
                            t.Commit();
                        }
                    }
                }            
            catch { 
                // add in later 
            }            
        }   

有人能找到我错的地方吗?如果你也需要解析器,我可以提供它们

0 个答案:

没有答案