EF Core 1.1是这种延迟加载

时间:2016-12-23 02:11:41

标签: c# entity-framework

我认为EF Core 1.1中的延迟加载尚不支持,但为什么我的代码在下面工作?我没有将Include函数用于ProductType,ProductSize等相关对象,但这不会导致任何错误..

有什么想法吗?

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

产品型号类如下。

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; }
}

1 个答案:

答案 0 :(得分:0)

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

var product = _context.Product.Find(1)
var productSizeName = product.ProductSize.ProductSizeName

会给您一个例外,因为product.ProductSizenull。要纠正此问题,您必须执行以下操作:

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

然后:

var productSizeName = product.ProductSize.ProductSizeName

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