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.

10
  • What type database are you using? Is it designed to be shared?
    – jdweng
    Feb 22, 2019 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, 2019 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, 2019 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, 2019 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, 2019 at 18:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.