【问题标题】:Why I am not able to get recently added objects (yet to be saved in database) from repository为什么我无法从存储库中获取最近添加的对象(尚未保存在数据库中)
【发布时间】:2014-09-25 11:41:02
【问题描述】:

我正在开发一个 asp.net mvc4 应用程序,其中实现了存储库和工作单元模式。我想要一些解决方案来从存储库中获取新添加的数据,这意味着我希望能够在将数据保存到数据库之前对其进行操作。

我了解到对象在 EF 更改跟踪机制中是可用的,因此它们应该是可用的,即使它尚未保存到数据库中。牢记这一点,我只会做以下事情

下面是我的控制器...

   Public void save(Order orderdata)
   {
      if(orderdata.Id > 0)
      {
            ....
            ....
            ....        // after validating the data's I will be calling the Save() service method which i mentioned below.
            save(orderdata,false);   //Calling service method to attach data to repository
      }
      //after attaching the orderdata to reposistory i am calling a private method to modify above freshly added orderdata(i.e., orderdata which is recently added into repository but not saved into database)
      UpdateOrder(order)
   }

下面是我的私有方法...

    private void UpdateOrder(Order order)
    {

        if (order != null)
        {
                var orderRequest = GetOrderRequest<Order>(x => x.TrackingNumber == order.TrackingNumber); //calling GetOrderRequest() service method which i mentioned below

                if (orderRequest != null)
                {
                    ...
                    ...// after some validations only i need to assign a status to newly added order.
                    orderRequest.Status = "assigned";
                }
            }
        }
    }

下面是我的服务方法实现...

  public void Save(Order obj, bool Commit = true)
    {
        if (obj.Id > 0)
        {
            // Adding Order obj to repository.
            OrderRepository.Attach(obj);
        }
        else
            OrderRepository.Add(obj);

        // Note: While I am calling my save() service method am passing commit as 'False' in order to prevent it from saving into database so that after some transaction i will finally save all the data into database.           
        if (Commit)
            Commit();
    }

    public IQueryable<T> GetOrderRequest<T>(Expression<Func<T, bool>> predicate = null) where T : WorkRequest
    {
        var query = OrderRepository.FindAll().OfType<T>(); 

        if (predicate != null)
            query = query.Where(predicate);

        return query; //Here I am expecting the newly added data
    }

现在我的问题是我想访问存储库中新添加的数据以更新我的订单状态。但我不能这样做,因为 GetOrderRequest 总是返回 null。是做错了什么还是有任何其他方法可以从存储库中获取新添加的实体。请给我一些建议...

【问题讨论】:

    标签: c# entity-framework asp.net-mvc-4 repository unit-of-work


    【解决方案1】:

    我在 Respository 类中使用它来访问上下文中的内容。 并不意味着它不持久。只是意味着它在上下文中。 但这对你来说可能仍然足够。 如果是数据库生成的 ID,则按键访问具有挑战性;-) 但除此之外,该集合非常有用。

    // ef offers  Local property, handy to see inside context.
    public ObservableCollection<TPoco> Local {
            get { return Context.Set<TPoco>().Local; }
    }
    

    所以你现在可以 linq 离开了...

    repository.Local.Where(t=>t.field == 'X' )
    

    【讨论】:

      猜你喜欢
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多