EntityFramework查询返回相同的对象关系 [英] EntityFramework Query Returns the same object for relationship

查看:76
本文介绍了EntityFramework查询返回相同的对象关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将标题命名为更有意义的标题时遇到了麻烦,所以我在问题中要有点冗长。

I'm having trouble phrasing the title into something that makes more sense, so I'm going to be a little verbose in my question.

我有这个EF查询:

    var allSocs = _db.Socs
    .Where(x => x.Soc6 != null)
    .Select(x => new SocModel {
        OccupationalEmploymentStatistics = x.OccupationalEmploymentStatistics
                    .Where(y => y.GeographyId == 25 && y.Release == "2015A01" && y.Naics == "000000")
                    .OrderByDescending(z => z.AnnualWageMedian),
        Soc6 = x.Soc6,
        OnetSocs = x.OnetSoc,
        Name = x.Name,
        Description = x.Description,
        EmploymentProjections = x.EmploymentProjections
                    .Where(y => y.GeographyId == "201400" && y.Naics == "000000")
    }).ToList();  

它返回一个名为 Soc 的对象列表s,具有一些原始属性和三个对象属性,分别是 Onetsocs OccupationalEmploymentStatistics EmploymentProjections

It's returning a list of Objects called Socs, who have some primitive properties and three Object properties, Onetsocs, OccupationalEmploymentStatistics, and EmploymentProjections.

我的行为是所有非对象字段都工作正常, EmploymentProjections OnetSocs

The behavior I'm having is that all of the non-object fields are working fine, as are EmploymentProjections and OnetSocs.

问题在于 OccupationalEmploymentStatistics 表现怪异。似乎EF仅获得一个 OccupationalEmploymentStastic 并将其应用于 _db.Socs

The issue is that OccupationalEmploymentStatistics is acting weird. It seems to be that EF is only getting one OccupationalEmploymentStastic and applying it to all records in _db.Socs.

如果设置断点并将鼠标悬停在 _db.Socs 上,则表明存在只有一个对象。但是, allSocs 中包含适当数量的Socs(841)。

If I set a breakpoint and mouse over _db.Socs, I'm shown that there is only one object. However, allSocs has the proper number of Socs in it (841).

我尝试删除 OrderByDescending 来查看是否是问题所在,但我仍然在相同的行为。

I've tried removing the OrderByDescending to see if maybe that was the issue, but I'm still getting the same behavior.

对于某些背景信息,我正在使用的表都是外部表,由于EF没接过它们,我不得不手动绘制关系。

For some background info, the tables that I'm working with are all external tables, and I had to draw the relationships manually since EF didn't pick up on them.

编辑:更多背景信息。如果我进行另一个查询,只是
var socs = _db.Socs.ToList(),则只有第一条记录具有任何 OccupationalEmploymentStatistics 。其他记录都没有

Some more background info. If I make another query that's just var socs = _db.Socs.ToList(), only the first record has any OccupationalEmploymentStatistics at all. None of the other records have any

推荐答案

,因为EF使用了身份映射模式。基本上,EF会话只会为给定的实体和ID存储一个实例,并且每当某些实体指向同一数据库表和主键时,它将始终重用同一实例。

Because EF uses the Identity Map pattern. Basically, an EF session will only store one instance for a given entity and id, and it will always reuse the same instance, whenever some entity points to the same DB table and primary key.

这篇关于EntityFramework查询返回相同的对象关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆