Why I am not able to get recently added objects (yet to be saved in database) from repository

五迷三道 提交于 2019-12-11 20:00:42

问题


I am developing an asp.net mvc4 application in which repository and unit of work pattern was implemented. I want some solution to get the newly added data from repository means i want to able to manipulate the data's before it saving into database.

I learned like the objects are available in the EF Change Tracking mechanism, so they should be available , even though it has not yet been saved to the database. By keeping this in mind only am doing the below stuffs

Below is my controller...

   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)
   }

Below is my private method...

    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";
                }
            }
        }
    }

Below is my service methods implementation...

  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
    }

Now my issue is i want to access the freshly added data in the repository to update my order status. But i am not able to do so because GetOrderRequest return always null. Is am doing anything wrong or is there any other way to get freshly added entities from repository. Please give me some suggestions on this...


回答1:


I use this in my Respository class to access the content inside the context. Does not mean it isnt persisted. Just means it is in the context. But that may still be enough for you. Accessing by key is challenging if it is DB generated IDs ;-) But otherwise the collection is very useful.

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

So you can now linq away...

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


来源:https://stackoverflow.com/questions/26037724/why-i-am-not-able-to-get-recently-added-objects-yet-to-be-saved-in-database-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!