2014-09-30 130 views
0

我想要訪問我的RegisterCountLog上的Register導航屬性。由於某些原因,它始終爲空。所以當我嘗試訪問RegisterName時,它始終爲空。EF導航屬性包括不加載

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

注意ServiceCollection.GetAll()將(或應該)包括Register。又名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]) 

它:

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(); 

一切完美,除了RegisterName總是空。如果我查看查詢Register也是空的。爲什麼不被加載?

這裏是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; } 
} 
+1

將寄存器虛擬爲啓用延遲加載或在將您的RegisterCountLog集合提取到加載時使用'.Include(x => x.Register)'方法。 – 2014-09-30 13:21:38

+0

'.GetAll()'應該預加載它(即時通訊使用存儲庫模式),我也嘗試過虛擬,但沒有工作..我再試一次以防萬一 – Chris 2014-09-30 13:24:13

回答

0

也許你正在使用預先加載,你必須指定.Include(x => x.Register).Include("Register"),取決於你的EF版本

如果你被點名了你的變量很大可能存在另一個問題(急切的加載對分組的東西不是很好)。一種方法是在選擇某個事物之前列舉查詢(列出)。 (如果沒有性能問題)

編輯:我看了你以前的評論,我真的覺得是該集團通過發行(集團通過改變你的LINQ結構,包括不工作anympore)

方法是這樣的:

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()]