0
最近添加的對象(尚未保存在數據庫中)我正在開發一個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。我在做任何錯誤的事情,還是有任何其他方式從知識庫中獲取新增實體。請給我一些建議,在這...