0

We save a list of records from one application by removing the list of items and saving the updated list (the list is always less than 10 items):

using (var context = GetContext())
{
    EFConfiguration.SuspendExecutionStrategy = true;
    using (var dbContextTransaction = context.Database.BeginTransaction())
    {
        try
        {
            context.ItemTable.RemoveRange(context.ItemTable.Where(d => d.ForeignKey == foreignKey));
            foreach (var item in items)
            {
                SetTimeStampFields(item);
                context.ItemTable.Add(item);
            }

            int i = await context.SaveChangesAsync();

            dbContextTransaction.Commit();

            EFConfiguration.SuspendExecutionStrategy = false;
        }
        catch (Exception)
        {
            dbContextTransaction.Rollback();
            EFConfiguration.SuspendExecutionStrategy = false;
            throw;
        }
    }
}

These changes show up in the database immediately. Then we pull the info from this table from another application:

var itemList = await context.ItemTable.ToListAsync();

The application seems to pickup the updated values when first deployed but afterwards they are stuck to the same values. I have tried adding AsNoTracking() before .ToListAsync() to try and circumvent any cache as this is readonly data but that did not work. The only solution that I have found that gets the data in real time is:

var itemList = await context.ItemTable.SqlQuery("Select * from TableName").ToListAsync();

The table in question holds records with a primary key, two foreign keys, and some timestamp data.

My question is why will the context not pickup the updated values from the table? This issue manifests in local and deployed environments. This is an ASP.NET Core application using Entity Framework 6. The database in question is a mature SQL Server 2012 database that is used across multiple applications without any issues such as this.

BHigzz
  • 45
  • 1
  • 5
  • What type database are you using? Is it designed to be shared? – jdweng Feb 22 '19 at 16:47
  • Its a SQL Server 2012 database and it is successfully used across multiple applications without any issues such as this. I have updated my post to include this. – BHigzz Feb 22 '19 at 16:50
  • I have similar issue when in c# I write to SQL Server while having SSMS open. For SSMS to get updates I sometimes have to close SSMS and reopen. – jdweng Feb 22 '19 at 17:10
  • 1
    Probably the context in the "another application" is long lived and tracks (caches) `ItemTable` objects. You can verify that by checking if `context.ItemTable.Local.Count == 0` before (and after) `var itemList = await context.ItemTable.AsNoTracking().ToListAsync();` – Ivan Stoev Feb 22 '19 at 18:20
  • The contexts in both applications follow the same pattern as above, in small need-to-use only using blocks. – BHigzz Feb 22 '19 at 18:49
  • What does `GetContext()` do? – Chris Pratt Feb 22 '19 at 19:06
  • Returns a new DbContext() with LazyLoadingEnabled and PorxyCreationEnabled both set to false. – BHigzz Feb 22 '19 at 19:16
  • @IvanStoev you were correct, that does happen, although I'm not sure where I can go with this information. – BHigzz Feb 22 '19 at 19:23
  • Make sure the context is created, used and disposed. And all queries use `AsNoTracking` option (because otherwise the `ItemTable` objects can be cached from the queries retrieving related data - you said it has FKs, hence potentially related entities with (collection) navigation properties) – Ivan Stoev Feb 22 '19 at 19:28
  • [This](https://stackoverflow.com/a/30170364/2557128) seems relevant? – NetMage Feb 22 '19 at 22:28

0 Answers0