簡體   English   中英

提交前從上下文中獲取數據

[英]Get data back from context before commit

customers customer = GetCustomer();
customer.UserId = userId;
customer.NeedToPayTax = true;
customer.IsApproved = false;
_customerRepository.Create(customer);

稍后在我們進行提交之前,我嘗試搜索此新customer

public int GetUncofirmedCount()
{
    var query = from p in _context.Set<customers>()
                where p.IsApproved == false
                select p;

    return query.Count();
}

但大小始終為0 如果我提交了,那么我可以看到正確的結果。 為什么? 即使尚未提交,如何從上下文取回數據?

未經測試的代碼:

    _customerRepository.ObjectStateManager.GetObjectStateEntries(EntityState.Added)
.Where(o => o.GetType() == typeof(Customer) && o => !o.IsApproved)
.Count();

或者,對於較新版本的EF,請查看Local

_customerRepository.Customers.Local
    .Where(o => o.GetType() == typeof(Customer) && o => !o.IsApproved)
    .Count();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM