コード例 #1
0
        public void Synchronize(Order_Detail orderDetail)
        {
            DbSet <OrderItem>    gameStoreDbSet = _gameStoreDbContext.Set <OrderItem>();
            DbSet <Order_Detail> northwindDbSet = _northwindDbContext.Set <Order_Detail>();

            if (orderDetail.IsProxy())
            {
                orderDetail = orderDetail.UnProxy(_northwindDbContext);
            }

            var northwindOrderItem = Mapper.Map <OrderItem>(orderDetail);

            OrderItem gameStoreOrderItem = gameStoreDbSet
                                           .FirstOrDefault(x => x.NorthwindOrderId == orderDetail.OrderID &&
                                                           x.NorthwindProductId == orderDetail.ProductID);

            if (gameStoreOrderItem == null)
            {
                _gameStoreDbContext.Set <OrderItem>().Add(northwindOrderItem);
            }
            else
            {
                if (gameStoreOrderItem.IsProxy())
                {
                    gameStoreOrderItem = gameStoreOrderItem.UnProxy(_gameStoreDbContext);
                }

                northwindOrderItem.OrderItemId = gameStoreOrderItem.OrderItemId;

                ICustomComparer comparer = _comparerFactory.GetComparer(typeof(OrderItem));

                if (!comparer.AreEqual(gameStoreOrderItem, northwindOrderItem))
                {
                    _gameStoreDbContext.Entry(gameStoreOrderItem).CurrentValues.SetValues(northwindOrderItem);
                }
            }
        }