【问题标题】:Why would my entity not return with updated values?为什么我的实体不会返回更新的值?
【发布时间】:2012-06-25 16:33:59
【问题描述】:

我在 EF Code First 中定义了一个带有 AccountNumber 属性的客户实体。我可以毫无问题地使用他们的 AccountNumber 获取和更新单个客户。但是,当我获得所有客户时,AccountNumber 已经旧了一段时间,然后最终在更改后更新。数据库始终返回正确的 AccountNumber 值。

我分析了 SQLServer,发现获取所有客户的调用确实进入了数据库,它返回了最新值,但来自 DbContext 的响应给出了旧的 AccountNumber。所以看起来 EFCF 正在缓存我的数据。

请帮忙。

更新:代码示例 -

        public IQueryable<Customer> GetCustomers()
    {
        return this.acmeDbContext.Customer
            .Include(x => x.IpAddressRange)
            ;
    }

    public Customer GetCustomer(Guid id)
    {
        var Customer = this.acmeDbContext.Customer
            .Include(x => x.Contacts)
            .Include(x => x.IpAddressRange)
            .OrderByDescending(x => x.Id)
            .FirstOrDefault(x => x.CustomerId == id);
        return Customer;
    }
    public void SaveCustomer(Customer Customer)
    {
        this.acmeDbContext.SaveChanges();
    }

【问题讨论】:

  • 请发布您保存/更新 AccountNumber 的代码以及您正在读取该值但不是最新的代码。
  • 是 EF 缓存数据。你必须明确地覆盖缓存,谷歌为 RefreshMode.StoreWins。由于没有代码,因此很难说在哪里应用它。

标签: sql-server entity-framework ef-code-first


【解决方案1】:

我最终使用 Reload 方法重新加载每个客户:

        if (noCache)
            customers.ToList().ForEach(x => acmeDbContext.Entry(x).Reload());

【讨论】:

    猜你喜欢
    • 2017-06-28
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2013-04-08
    相关资源
    最近更新 更多