我有 MVC Web 应用程序项目,我想要延迟加载。
我在导航属性中的模型对象中使用virtual
了关键字,但我想在getmanualylist()
我的存储库层中调用它的一种方法。我可以通过方法获取该对象的相关对象include
。
我在许多网站中看到导航字段所在的位置virtual
,它们可以include
在一段代码中使用来预先加载对象,但我不能,因为当我在查询中运行我的方法时,我看到它返回了正确的查询。但是当我想查看列表时,它会返回null
相关对象,它显示急切加载不起作用。
我不知道为什么会出现这个问题以及出了什么问题?
public IQueryable<InvoiceItem> GetManuallylist()
{
IQueryable<InvoiceItem> query = null;
query = this.dbContext.Set<InvoiceItem>().Include("PartLocation1").Include("PartContractInfo1");
var list = this.dbContext.Set<InvoiceItem>().Include("PartContractInfo1").ToList();
var list2 = this.dbContext.Set<InvoiceItem>().Include("PartLocation1").ToList();
var list23 = this.dbContext.Set<InvoiceItem>().Include("PartBooklet2").ToList();
return query;
}
这是我的 InvoiceItemModel 的一段代码
public virtual Invoice Invoice1 { get; set; } // Invoice -
public virtual SparePart SparePart2 { get; set; } // SparePart -
public virtual PartBooklet PartBooklet2 { get; set; } // PartBooklet
public virtual PartLocation PartLocation1 { get; set; } // PartLocation -
public virtual PartContractInfo PartContractInfo1 { get; set; } // ContractInfo -
public virtual Project Project1 { get; set; } // Project
public virtual BaseData BaseData1 { get; set; }
public virtual BaseData BaseDataTakingplace { get; set; }
public virtual Personel PersonelTroubleshooter { get; set; }
public virtual Personel PersonelMasterApplier { get; set; }
public virtual Personel PersonelBuildingExpert { get; set; }
这是查询:
{SELECT
[Extent1].[id] AS [id],
[Extent1].[invoice] AS [invoice],
[Extent1].[part_code] AS [part_code],
[Extent1].[spare_part] AS [spare_part],
[Extent1].[ProjectId] AS [ProjectId],
[Extent1].[part_count] AS [part_count],
[Extent1].[deliveris_partcount] AS [deliveris_partcount],
[Extent1].[descr] AS [descr],
[Extent1].[part_location] AS [part_location],
[Extent1].[contract_info] AS [contract_info],
[Extent1].[HasSerial] AS [HasSerial],
[Extent1].[HasUselesSpiece] AS [HasUselesSpiece],
[Extent1].[HasQCConfirmation] AS [HasQCConfirmation],
[Extent1].[HasRepair] AS [HasRepair],
[Extent1].[faulty_Reason] AS [faulty_Reason],
[Extent1].[Troubleshooter] AS [Troubleshooter],
[Extent1].[MasterApplier] AS [MasterApplier],
[Extent1].[BuildingExpert] AS [BuildingExpert],
[Extent1].[Takingplace] AS [Takingplace],
[Extent1].[Warrantyperiod] AS [Warrantyperiod],
[Extent1].[DeliveryDateForBuilding] AS [DeliveryDateForBuilding],
[Extent2].[id] AS [id1],
[Extent2].[train] AS [train],
[Extent2].[wagon_number] AS [wagon_number],
[Extent2].[install_date] AS [install_date],
[Extent2].[pickup_date] AS [pickup_date],
[Extent2].[descr] AS [descr1],
[Extent3].[id] AS [id2],
[Extent3].[contract] AS [contract],
[Extent3].[project] AS [project],
[Extent3].[contractor] AS [contractor],
[Extent3].[SupplierId] AS [SupplierId],
[Extent3].[SupplyType] AS [SupplyType],
[Extent3].[request] AS [request],
[Extent3].[request_date] AS [request_date],
[Extent3].[SparePartStatusValue] AS [SparePartStatusValue],
[Extent3].[unit_price] AS [unit_price],
[Extent3].[total_cost] AS [total_cost],
[Extent3].[descr] AS [descr2],
[Extent3].[BaseData_Id] AS [BaseData_Id]
FROM [dbo].[invoice_item] AS [Extent1]
LEFT OUTER JOIN [dbo].[part_location] AS [Extent2] ON [Extent1].[part_location] = [Extent2].[id]
LEFT OUTER JOIN [dbo].[part_contract_info] AS [Extent3] ON [Extent1].[contract_info] = [Extent3].[id]}