Entity Framework Caching Issue

后端 未结 12 2187
無奈伤痛
無奈伤痛 2020-12-08 00:19

I am new to Entity Framework.

I have get to some values in my database using EF. It returns perfectly, and the values are shown in labels. But When I delete all valu

相关标签:
12条回答
  • 2020-12-08 00:40

    In my case it was a bad connection string. Entity looked like it was doing great because it never complained until I made the context local and it finally gave me the error message.

    0 讨论(0)
  • 2020-12-08 00:44

    Below code helped my object to be refreshed with fresh database values. The Entry(object).Reload() command forces the object to recall database values

    GM_MEMBERS member = DatabaseObjectContext.GM_MEMBERS.FirstOrDefault(p => p.Username == username && p.ApplicationName == this.ApplicationName);
    DatabaseObjectContext.Entry(member).Reload();
    
    0 讨论(0)
  • 2020-12-08 00:48

    Couple things you can do.

    1. Use a new context. The cached entities are stored in the context. Using a new context prevents it from using the cache.
    2. If you really want a global/long lasting context, you have two sub options: a.) always call the Reload method. db.Entry(entity).Reload() ... this forces the context to reload that entity. b.) use a SqlDependency object to detect when records change and reload the entities as needed. https://code.msdn.microsoft.com/How-to-use-SqlDependency-5c0da0b3
    0 讨论(0)
  • 2020-12-08 00:48

    EF works differently with find method which gives data from context .Rest other query are from db.If an object is already in the context, the existing object is returned (the current and original values of the object's properties in the entry are not overwritten with database values). A query is executed against the database when:

    Microsoft link

    It is enumerated by a foreach (C#) or For Each (Visual Basic) statement. It is enumerated by a collection operation such as ToArray, ToDictionary, or ToList. LINQ operators such as First or Any are specified in the outermost part of the query. The following methods are called: the Load extension method on a DbSet, DbEntityEntry.Reload, and Database.ExecuteSqlCommand. When results are returned from the database, objects that do not exist in the context are attached to the context. If an object is already in the context, the existing object is returned (the current and original values of the object's properties in the entry are not overwritten with database values).

    When you perform a query, entities that have been added to the context but have not yet been saved to the database are not returned as part of the result set. To get the data that is in the context, see Local Data.

    If a query returns no rows from the database, the result will be an empty collection, rather than null.

    0 讨论(0)
  • 2020-12-08 00:52

    If you know that changes happened outside of EF and want to refresh your ctxt for a specific entity, you can call ObjectContext.Refresh

    datamodel.Refresh(RefreshMode.StoreWins, orders);
    

    If this seems like it will be a common occurance, you should disable object caching in your queries:

    SchoolBriefcaseEntities datamodel = new SchoolBriefcaseEntities();
    datamodel.tblCities.MergeOption = MergeOption.NoTracking; 
    

    or for to turn off object level caching for specific Entity,

    Context.Set<Compliances>().AsNoTracking();
    
    0 讨论(0)
  • 2020-12-08 00:53

    I think what you need is GetDatabaseValues(). It is used like:

    context.Entry(/*your entry*/).GetDatabaseValues();
    

    Information below is from msdn:

    The current values are the values that the properties of the entity currently contain. The original values are the values that were read from the database when the entity was queried. The database values are the values as they are currently stored in the database. Getting the database values is useful when the values in the database may have changed since the entity was queried such as when a concurrent edit to the database has been made by another user.

    0 讨论(0)
提交回复
热议问题