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; }
}
將寄存器虛擬爲啓用延遲加載或在將您的RegisterCountLog集合提取到加載時使用'.Include(x => x.Register)'方法。 – 2014-09-30 13:21:38
'.GetAll()'應該預加載它(即時通訊使用存儲庫模式),我也嘗試過虛擬,但沒有工作..我再試一次以防萬一 – Chris 2014-09-30 13:24:13