为什么我不能从存储库中获得最近添加的对象(尚未保存在数据库中) [英] Why I am not able to get recently added objects (yet to be saved in database) from repository

查看:174
本文介绍了为什么我不能从存储库中获得最近添加的对象(尚未保存在数据库中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个asp.net mvc4应用程序,其中已经实现了存储库和工作模式。我想要一些解决方案从存储库获取新添加的数据意味着我想要在数据保存之前操纵数据。



我学到的对象可以在EF变更跟踪机制,因此即使尚未保存到数据库中也可以使用它们。通过记住这一点,只是在做以下的东西



以下是我的控制器...

  public void save(Order orderdata)
{
if(orderdata.Id> 0)
{
....
。 ...
.... //验证数据后,我将调用Save()服务方法,我在下面提到。
save(orderdata,false); //调用服务方法将数据附加到存储库
}
//在将orderdata附加到reposistory之后,我正在调用私有方法来修改新增的orderdata(即最近添加到存储库中的orderdata)没有保存到数据库中)
UpdateOrder(order)
}

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

  private void UpdateOrder(Order order)
{

if (order!= null)
{
var orderRequest = GetOrderRequest< Order>(x => x.TrackingNumber == order.TrackingNumber); //调用GetOrderRequest()服务方法,我在下面提到

if(orderRequest!= null)
{
...
... //之后仅验证我需要为新添加的订单分配状态。
orderRequest.Status =assign;
}
}
}
}

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

  public void Save(Order obj,bool Commit = true)
{
if(obj.Id> 0)
{
//将Order obj添加到存储库。
OrderRepository.Attach(obj);
}
else
OrderRepository.Add(obj);

//注意:当我调用我的save()服务方法时,我将commit提交为False,以防止将其保存到数据库中,以便在一些事务之后,我将最终保存所有数据进入数据库。
if(Commit)
Commit();
}

public IQueryable< T> GetOrderRequest< T>(表达式 {
var query = OrderRepository.FindAll()。OfType< T>

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

返回查询; //这里我期待新添加的数据
}

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

解决方案

我在我的 Respository 类访问上下文内容。
并不意味着它不会持久化。只是意味着它在上下文中。
但是这可能还是足够的。
如果是DB生成的ID,则访问密钥是具有挑战性的;-)
否则收集非常有用。

  // ef提供本地属性,方便查看内部上下文。 
public ObservableCollection< TPoc​​o>本地{
get {return Context.Set< TPoc​​o>()。Local;
}

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

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

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...

解决方案

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

这篇关于为什么我不能从存储库中获得最近添加的对象(尚未保存在数据库中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆