简体   繁体   English

查询未返回实体

[英]Entity not returned from query

I have this problem where I have 2 entities connected by foreign key.我有这个问题,我有 2 个通过外键连接的实体。

  • AEntity: id, idOfEntityB (foreign key, constraint), fields... AEntity:id、idOfEntityB(外键、约束)、字段...
  • BEntity: id, fields... BEntity:id,字段...

I save both of them to the database with SaveChanges() , later when I try to get AEntity 's idOfEntityB , I succeed but when I try to get BEntity according to the id I got from AEntity , I get nothing:我使用SaveChanges()将它们都保存到数据库中,稍后当我尝试获取AEntityidOfEntityB时,我成功了,但是当我尝试根据从BEntity获得的 id 获取AEntity时,我什么也没得到:

context.AEntities.Add(new AEntity {
   BEntity = new BEntity { ... }
});
context.SaveChanges();
. 
. 
. 
var id1 = context.AEntities.Select(x => x.idOfEntityB);
var bEntities = context.BEntities.Where(x => id1.Contains(x.id));

bEntities has nothing in it. bEntities什么都没有。 but the fact I was able to have values in id1 is even more confusing since they have foreign key relations (with constraint) and furthermore, id could not be created if it was not saved to the DB.但是我能够在 id1 中有值的事实更加令人困惑,因为它们具有外键关系(有约束),此外,如果没有将 id 保存到数据库中,则无法创建它。

Later, when I look in the DB I see both entities as should be.后来,当我查看数据库时,我看到了两个实体。

It happens sometimes and I cant reproduce the problem, I cant give more then this as an example since there's a lot of code, I believe it has something to do with caching, and therefore would like to ask if something like that is possible or not and how.它有时会发生,我无法重现这个问题,我不能给出更多的例子,因为有很多代码,我相信它与缓存有关,因此想问这样的事情是否可能如何。

is there a way entities are saved to the DB while the context (a different one used from the context that saved) does not hold all of them in completion?有没有办法将实体保存到数据库中,而上下文(与保存的上下文不同)不能完成所有实体?

This is likely the issue you are encountering if you are relying on seeing changes between state changes between different DbContext instances.如果您依赖于查看不同 DbContext 实例之间的 state 变化之间的变化,这可能是您遇到的问题。 When a DbContext has loaded entities, then another DbContext instance makes changes to those records or the records change behind the scenes in the database, that original DbContext will not refresh the entities from the database.当一个 DbContext 加载了实体,然后另一个 DbContext 实例对这些记录进行更改或数据库中的记录在后台更改时,原始 DbContext 将不会刷新数据库中的实体。

EF does support the ability to reload entities from the database, but when dealing with child collections it gets a bit more complicated to perform a full refresh. EF 确实支持从数据库重新加载实体的能力,但是在处理子 collections 时,执行完全刷新会变得有点复杂。 You effectively need to tell the DbContext to forget all of the child collections, stop tracking the parent, clear the parent's child collection, then re-attach and reload the child collection.您实际上需要告诉 DbContext 忘记所有子集 collections,停止跟踪父集,清除父集的子集,然后重新附加并重新加载子集。 I recently covered this in the answer for this question: Replacing a entity collection in Entity Framework Core causes DbContext to fetch the new values when not saved to db.我最近在这个问题的答案中介绍了这一点: Replaceing a entity collection in Entity Framework Core 会导致 DbContext 在未保存到 db 时获取新值。 How to reload the collection? 如何重新加载收藏?

Ultimately a DbContext lifespan should be kept as short as possible.最终,DbContext 的生命周期应尽可能短。

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

相关问题 从实体框架查询返回的两个List &lt;&gt;对象的外部联接 - Outer Join on two List<> objects returned from entity framework query 使用从实体框架查询返回的数据填充ViewModel - Populate ViewModel with data returned from Entity Framework query 避免实体框架查询返回的列表导致内存泄漏 - Avoiding memory leak from a list returned by an entity framework query 查询的LinqToEntities代码需要为每个根节点返回一个实体吗? - LinqToEntities code for Query needing one entity returned foreach root node? 如何从ConcerObjectID查询实体? - how to query entity from RegardingObjectID? 编辑从 SQL 查询返回的值 - Edit the values returned from an SQL Query 反序列化从SQL查询返回的XML对象? - deserialize an XML object returned from SQL query? 实体框架从linq select返回的数据不正确 - Entity framework incorrect data returned from linq select 无法读取使用实体框架从存储过程返回的值 - Unable to read value returned from a stored procedure using Entity Framework 如何从实体数据源绑定到detailsview的返回实体? - How do I get the returned entity from a entitydatasource binding to a detailsview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM