C # Entity Framework Linq Query not recognizing new changes

I am using C #,. NET (4.0) and Entity Framework to connect to SQL CE 4.0.

I am querying some objects with specific properties, but the query only returns objects matching the search criteria, only if that data has already been saved in the database, which is not so problematic, the big problem is if the data is changed but not saved yet into the database, it will still match the search criteria.


var query = from location in mainDBContext.Locations
            where location.InUse == true
            select location;


This query also returns objects where location.InUse = false if InUse was true when loaded from the database and then changed later in code.

This is a screen capture from one of the query result objects.

enter image description here

I really don't understand why he does this. I would understand if this query would always query the database and I would get an older version of this object (thus InUse would be true).

Thanks for your time and answers.


source to share

3 answers

This is how EF works internally.

Each object, uniquely identified by its key, can be tracked by the context only once — called an identity card . Therefore, it doesn't matter how many times you run the query. If a query returns trackable entities, and if it is re-executed in the same instance of the context, it always returns the same instance.

If the instance has been modified in the application, but not saved to the database, your query will be executed against the database, where the persistent state will be checked, but the materialization process will by default use the current data from the application rather than the data retrieved from the database. You can force the query to return state from the database (by setting mainDBContext.Locations.MergeOption = MergeOption.OverwriteChagens

), but because of the identity card, your current changes will be lost.



I'm not really sure what exactly your problem is, but I think you should know this:

  • Such a request always returns data that is transferred to the database. When some objects in the code change, but they are not sent to the database, the LINQ query will query the data from the database without changes in the code.

  • LINQ queries use deferred execution, so your query variable is not a list of results, it is just a query definition that is evaluated every time results are needed. You should add .ToList()

    to evaluate this query and get a list of results in that specific line of code.

Example for .ToList()


var query = (from location in mainDBContext.Locations
                 where location.InUse == true
                 select location).ToList();




I just stumbled upon the same thing. It's a little messy, but another option is to examine the local cache. You can do this for example:

var query = from location in mainDBContext.Locations.Local
            where location.InUse == true
            select location;


This will only use the local cache, not stored in the database. A combination of local queries and database queries will get you what you want.



All Articles