简体   繁体   English

C#Entity Framework Linq查询无法识别新的更改

[英]C# Entity Framework Linq Query not recognizing new changes

I'm using C#, .NET (4.0) and Entity Framework to connect to SQL CE 4.0. 我正在使用C#,.NET(4.0)和Entity Framework连接到SQL CE 4.0。

I query some objects with specific properties, but the query returns only objects that meet search criteria only if that data was already saved to database, which is not that problematic, bigger problem is that if data is changed, but not yet saved to database it will still meet search criteria. 我查询了一些具有特定属性的对象,但只有当数据已经保存到数据库时,查询才返回满足搜索条件的对象,这不是问题,更大的问题是如果数据被更改但尚未保存到数据库中仍将符合搜索条件。

Example: 例:

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

This query returns also objects where location.InUse = false if InUse was true when loaded from DB and then changed later on in code. 如果InUse在从DB加载时为真,然后在代码中更改,则此查询还返回其中location.InUse = false的对象。

This is screen capture from one of the query results objects. 这是来自其中一个查询结果对象的屏幕截图。

在此输入图像描述

I really don't understand why it does this. 我真的不明白为什么会这样做。 I would understand if this query would always query database and I would get the older version of this object (thus InUse would be true). 我会理解,如果这个查询总是查询数据库,我会得到这个对象的旧版本(因此InUse将是真的)。

Thank you for your time and answers. 谢谢你的时间和答案。

That is how EF works internally. 这就是EF内部运作的方式。

Every entity uniquely identified by its key can be tracked by the context only once - that is called identity map . 由其键唯一标识的每个实体只能由上下文跟踪一次 - 称为身份映射 So it doesn't matter how many times did you execute the query. 因此,执行查询的次数并不重要。 If the query is returning tracked entities and if it is repeatedly executed on the same context instance it will always return the same instance. 如果查询返回跟踪的实体,并且如果它在同一个上下文实例上重复执行,则它将始终返回相同的实例。

If the instance was modified in the application but not saved to the database your query will be executed on the database where persisted state will be evaluated but materialization process will by default use the current data from the application instead of 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 identity map your current modifications will be lost. 您可以强制查询从数据库返回状态(通过设置mainDBContext.Locations.MergeOption = MergeOption.OverwriteChagens ),但由于身份映射,您当前的修改将丢失。

I just ran into the same thing myself. 我自己也碰到了同样的事情。 It's a bit 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 saved to the database. 这将仅使用未保存到数据库的本地缓存。 A combination of local and database queries should enable you to get what you want. 本地和数据库查询的组合应该可以让您获得所需的内容。

I'm not really sure what exactly your problem is, but I think you have to know this: 我不确定你的问题到底是什么,但我想你必须知道这个:

  • That kind of query always return data that is submitted into DB. 这种查询总是返回提交到DB的数据。 When you change some entities in your code but they are not submitted into database the LINQ query will query the data from database, without your in-code changes. 当您更改代码中的某些实体但未将其提交到数据库时,LINQ查询将查询数据库中的数据,而不进行代码更改。

  • LINQ queries use Deferred Execution, so your 'query' variable is not a list of results, it's just a query definition that is evaluated each time results are needed. LINQ查询使用延迟执行,因此您的“查询”变量不是结果列表,它只是每次需要结果时评估的查询定义。 You should add .ToList() to evaluate that query and get a list of results in that certain line of code. 您应该添加.ToList()来评估该查询并获取该特定代码行中的结果列表。

An example for .ToList() : .ToList()一个例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM