Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF Core 1.1就是这种延迟加载_C#_Entity Framework - Fatal编程技术网

C# EF Core 1.1就是这种延迟加载

C# EF Core 1.1就是这种延迟加载,c#,entity-framework,C#,Entity Framework,我认为EFCore1.1中的延迟加载还不受支持,但为什么我下面的代码可以工作呢?我没有对ProductType、ProductSize等相关对象使用Include函数,但这不会导致任何错误 有什么想法吗 List<ProductViewModel> data = await (from s in _context.Product where ((categorySearchValue == -1

我认为EFCore1.1中的延迟加载还不受支持,但为什么我下面的代码可以工作呢?我没有对ProductType、ProductSize等相关对象使用Include函数,但这不会导致任何错误

有什么想法吗

List<ProductViewModel> data = await (from s in _context.Product
                                             where ((categorySearchValue == -1 || s.ProductCategoryId == categorySearchValue)
                                             && (typeSearchValue == -1 || s.ProductTypeId == typeSearchValue)
                                             && (sizeSearchValue == -1 || s.ProductSizeId == sizeSearchValue))
                                             select new ProductViewModel
                                             {
                                                 ProductID = s.ProductId,
                                                 ProductName = s.ProductName,
                                                 ProductCategory = s.ProductCategory.ProductCategoryName,
                                                 ProductType = s.ProductType.ProductTypeName,
                                                 ProductSize = s.ProductSize.ProductSizeName,
                                                 CurrentQuantity = s.CurrentQuantity,
                                                 QuantityPerBox = s.QuantityPerBox,
                                                 AvgUnitCost = s.AvgUnitCost,
                                                 MainVendor = s.MainSeller.CustomerName,
                                                 IncludePBox = (Convert.ToBoolean(s.IsPboxIncluded) ? "Y" : "N"),
                                                 Disabled = (s.Inactive ? "Y" : "N"),
                                                 IsTaxProduct = (Convert.ToBoolean(s.IsTaxProduct) ? "Y" : "N"),
                                                 UnitType = s.ProductUnitType.ProductUnitTypeName
                                             }).ToListAsync();
List data=wait(来自_context.Product中的s
其中((categorySearchValue==1 | | s.ProductCategoryId==categorySearchValue)
&&(typeSearchValue==-1 | | s.ProductTypeId==typeSearchValue)
&&(sizeSearchValue==-1 | | s.ProductSizeId==sizeSearchValue))
选择新产品视图模型
{
ProductID=s.ProductID,
ProductName=s.ProductName,
ProductCategory=s.ProductCategory.ProductCategoryName,
ProductType=s.ProductType.ProductTypeName,
ProductSize=s.ProductSize.ProductSizeName,
CurrentQuantity=s.CurrentQuantity,
QuantityPerBox=s.QuantityPerBox,
AvgUnitCost=s.AvgUnitCost,
MainVendor=s.MainSeller.CustomerName,
IncludeBox=(Convert.ToBoolean(s.IsPboxIncluded)?“Y”:“N”),
禁用=(s.Inactive?“Y”:“N”),
IsTaxProduct=(Convert.ToBoolean(s.IsTaxProduct)?“Y”:“N”),
UnitType=s.ProductUnitType.ProductUnitTypeName
}).ToListAsync();
产品型号分类如下

public partial class Product
{
    public Product()
    {
        TransactionLine = new HashSet<TransactionLine>();
        TransactionLog = new HashSet<TransactionLog>();
    }

    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public int CurrentQuantity { get; set; }
    public int AvgUnitCost { get; set; }
    public int QuantityPerBox { get; set; }
    public int ProductCategoryId { get; set; }
    public int? ProductSizeId { get; set; }
    public int? ProductTypeId { get; set; }
    public int ProductUnitTypeId { get; set; }
    public int MainSellerId { get; set; }
    public bool IsTaxProduct { get; set; }
    public bool IsPboxIncluded { get; set; }
    public bool Inactive { get; set; }
    public DateTime DateUpdated { get; set; }

    public virtual ICollection<TransactionLine> TransactionLine { get; set; }
    public virtual ICollection<TransactionLog> TransactionLog { get; set; }
    public virtual Customer MainSeller { get; set; }
    public virtual ProductCategory ProductCategory { get; set; }
    public virtual Product ProductNavigation { get; set; }
    public virtual Product InverseProductNavigation { get; set; }
    public virtual ProductSize ProductSize { get; set; }
    public virtual ProductType ProductType { get; set; }
    public virtual ProductUnitType ProductUnitType { get; set; }
}
公共部分类乘积
{
公共产品()
{
TransactionLine=新的HashSet();
TransactionLog=新的HashSet();
}
public int ProductId{get;set;}
公共字符串ProductName{get;set;}
公共int CurrentQuantity{get;set;}
public int AvgUnitCost{get;set;}
public int QuantityPerBox{get;set;}
public int ProductCategoryId{get;set;}
public int?ProductSizeId{get;set;}
public int?ProductTypeId{get;set;}
public int ProductUnitTypeId{get;set;}
public int MainSellerId{get;set;}
公共布尔IsTaxProduct{get;set;}
公共bool IsPboxIncluded{get;set;}
公共bool非活动{get;set;}
public DateTime DateUpdated{get;set;}
公共虚拟ICollection事务线{get;set;}
公共虚拟ICollection事务日志{get;set;}
公共虚拟客户主卖家{get;set;}
公共虚拟产品类别产品类别{get;set;}
公共虚拟产品ProductNavigation{get;set;}
公共虚拟产品InverseProductNavigation{get;set;}
公共虚拟ProductSize ProductSize{get;set;}
公共虚拟ProductType ProductType{get;set;}
公共虚拟ProductUnitType ProductUnitType{get;set;}
}

这不是延迟加载。延迟加载意味着您可以在执行查询后访问引用的属性。例如,假设您有一个id为1的产品,该代码为:

var product = _context.Product.Find(1)
var productSizeName = product.ProductSize.ProductSizeName
将给您一个异常,因为
product.ProductSize
null
。要更正此问题,您必须执行以下操作:

_context.Entry(product).Reference(x => x.ProductSize).Load()
然后:

var productSizeName = product.ProductSize.ProductSizeName

如果您在创建查询时使用了
product.ProductSize
,如您的示例所示,它不会给您一个错误,因为它将使用
internal join
获取数据。

根据,EF core不支持延迟加载,所以答案是否定的。是的,我知道它不受支持,但显然上面的代码是惰性加载的。不使用include函数,我可以访问相关对象,请参见上面的代码。或者我理解错了?因为我们看不到您的实体,或者您是如何创建模型的,或者您是如何创建上下文的,等等,所以真的很难说。我用产品类更新了