简体   繁体   English

EF导航属性包括不加载

[英]EF navigation property include doesn't load

I am trying to access the Register navigational property on my RegisterCountLog .. For some reason it is always null. 我试图访问Register在我的导航性能RegisterCountLog ..出于某种原因,它始终为空。 So when I try access RegisterName it is always null. 因此,当我尝试访问RegisterName时,它始终为null。

public IQueryable<RegisterCountLog> GetCountsForDevice(long deviceSerial)
{
    return this.ServiceCollection.GetAll(c => c.Register)
               .Where(c => c.DeviceSerial == deviceSerial).AsNoTracking();
}

Note that ServiceCollection.GetAll() will (or should) include the Register . 请注意, ServiceCollection.GetAll()将(或应该)包括Register The query after that specific bit aka queryableLogs is 该特定位又称为queryableLogs之后的queryableLogs

SELECT 
    [Extent2].[Contribution] AS [Contribution], 
    [Extent1].[DeviceSerial] AS [DeviceSerial], 
    [Extent1].[LogEntryID] AS [LogEntryID], 
    [Extent1].[RegisterId] AS [RegisterId], 
    [Extent1].[Value] AS [Value], 
    [Extent1].[Timestamp] AS [Timestamp], 
    [Extent2].[RegisterId] AS [RegisterId1], 
    [Extent2].[DeviceSerial] AS [DeviceSerial1], 
    [Extent2].[RegisterName] AS [RegisterName], 
    [Extent2].[ZoneEntrance_ZoneEntranceID] AS [ZoneEntrance_ZoneEntranceID]
    FROM  [dbo].[RegisterCountLogs] AS [Extent1]
    INNER JOIN [dbo].[Registers] AS [Extent2] 
          ON  ([Extent1].[RegisterId] = [Extent2].[DeviceSerial]) 
          AND ([Extent1].[DeviceSerial] = [Extent2].[RegisterId])

Its used like so: 其用法如下:

var queryableLogs = countLogService.GetCountsForDevice(serialNumber);
var groupedLogs= gLogs
    .Select(r => new Register
    {
        RegisterId = r.FirstOrDefault().RegisterId,
        RegisterName = r.FirstOrDefault().Register.RegisterName, // Here!
        DeviceSerial = r.FirstOrDefault().DeviceSerial,
        CountLogs = r.GroupBy(rc => 
                              new {Day = SqlFunctions
                                        .DatePart("dy", rc.Timestamp)})
                     .Select(rl => new RegisterCountLog()
                                   {
                                     Value = rl.to FirstOrDefault().Value,
                                     Timestamp = rl.FirstOrDefault()
                                                   .Timestamp,
                                   })
                     .OrderByDescending(l => l.Timestamp)
                     .ToList()
    })
    .Where(r=>r.CountLogs.Count() > 0)
    .OrderByDescending(r => r.RegisterId)
    .ToList();

Everything works perfectly except the RegisterName is always null. RegisterName始终为null之外,其他所有内容均正常运行。 If I check out the query Register is null too. 如果我签出查询Register也为空。 Why isnt it being loaded? 为什么不加载?

Here are the EF classes 这是EF课程

public class RegisterCountLog
{
    [Key, Column(Order = 1)]
    [ForeignKey("Register")]
    public long DeviceSerial { get; set; }

    [Key, Column(Order = 2)]
    public long LogEntryID { get; set; }

    [Key, Column(Order = 3)]
    [ForeignKey("Register")]
    public long RegisterId { get; set; }

    public long Value { get; set; }

    public DateTime Timestamp { get; set; }

    public Register Register { get; set; }
}

public class Register
{
    public enum ContributionType
    {
        FlowIn,
        FlowOut
    }

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Key, Column(Order = 1)]
    public long RegisterId { get; set; }

    [Key]
    [ForeignKey("Device"), Column(Order = 2)]
    public long DeviceSerial { get; set; }

    [StringLength(50)]
    public string RegisterName { get; set; }

    public ContributionType Contribution { get; set; }    

    public virtual Device Device { get; set; }
}

Probably you are using eager loading and you must specify .Include(x => x.Register) or .Include("Register") , depends on your EF version 可能您正在使用.Include(x => x.Register)加载,并且必须根据您的EF版本指定.Include(x => x.Register).Include("Register")

If you named your variables great there can be another issue (eager loading isn't great with grouped things). 如果您将变量命名为great,那么可能会出现另一个问题(对于分组的东西,急于加载不是很好)。 One approach is to enumerate the query (to list) before selecting something. 一种方法是在选择某些内容之前枚举查询(列出)。 (if there aren't performance problems) (如果没有性能问题)

EDIT : as i read your previous comment, i really think is the group by issue ( group by changes your linq structure and include isn't working anympore ) 编辑:当我阅读您的先前评论时,我真的认为是按问题分组(按分组更改您的linq结构并包括不起作用anympore)

Approach is this : 方法是这样的:

var groupedPersons     = db.Appointments
                        .Include_Service()
                        .Include_Location()
                        .Include_Room()
                        .IncludeEhr_Patient()
                        .IncludeEhr_Person()
                        .IncludeEhr_Personal()
                        .ToList() // see that i enumerate before i group by
                        .GroupBy(x => x.PersonId).Select()[.ToList()] 

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

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