Entity Framework Core LazyLoading 返回 null

问题描述 投票:0回答:2

我正在处理一个项目,我需要使用延迟加载,我安装了包并配置,查询工作但关系列返回空。如何使用延迟加载?

已安装 Microsoft.EntityFrameworkCore.Proxies(版本 5.0.7)

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer("ConnectionString");
                optionsBuilder.UseLazyLoadingProxies(true);

            }
        }

我的模型(我用 DB First 制作)

 public partial class Customer
    {
        public Customer()
        {
            Orders = new HashSet<Order>();
         
        }

        public int CustomerId { get; set; }
        public int? CompanyId { get; set; }
        public string Name { get; set; }
        public string LastName { get; set; }
        public string FullName { get; set; }
        public string Phone { get; set; }
        public string Mail { get; set; }

        virtual public Company Company { get; set; }
        virtual public ICollection<Order> Orders { get; set; }
    }
    
public partial class Company
    {
        public Company()
        {
            Customers = new HashSet<Customer>();
        }

        public int CompanyId { get; set; }
        public string Name { get; set; }
        public string TaxNumber { get; set; }

        public virtual ICollection<Customer> Customers { get; set; }
    }

使用方法:

public List<Customer> GetCustomers()
        {
            using (dbContext context = new dbContext())
            {
               return context.Customers.ToList();
            }
        }
c# .net entity-framework .net-core
2个回答
1
投票

根据文档,延迟加载应该是这样实现的

也安装

Microsoft.EntityFrameworkCore.Abstractions
包并更改实现:

public partial class Customer
{
    private ICollection<Orders> _orders;

    public Customer()
    {
        Orders = new HashSet<Order>();
    }

    public Customer(ILazyLoader lazyLoader)
    {
        LazyLoader = lazyLoader;
    }

    private ILazyLoader LazyLoader { get; set; }

    public int CustomerId { get; set; }
    public int? CompanyId { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public string FullName { get; set; }
    public string Phone { get; set; }
    public string Mail { get; set; }

    virtual public Company Company { get; set; }

    public ICollection<Order> Orders
    {
        get => LazyLoader.Load(this, ref _orders);
        set => _orders = value;
    }
}

迭代订单数据后将获取

using(var db = new YourContext())
{
    var customers = db.Customers.ToList();
    foreach(var customer in customers )
    {
        Console.WriteLine($"Customer: {customer.Name} {customer .LastName}");
        foreach(var order in customer.Ortders) // lazy loading initiated
        {
            Console.WriteLine($"\t{order.Id}");
        }
    }
}

0
投票

当 EF 尝试从您的类创建对象时,它调用没有参数的模型构造函数,然后

lazyLoader
服务未填充。 你最常删除空的构造函数并只保留那个
lazyLoader
:

public Customer(ILazyLoader lazyLoader)
{
    LazyLoader = lazyLoader;
}

你的模型类最像这样:

public partial class Customer
{
    private ICollection<Orders> _orders;
    private ILazyLoader LazyLoader { get; set; }

    public Customer(ILazyLoader lazyLoader)
    {
        LazyLoader = lazyLoader;
    }
 
    public int CustomerId { get; set; }
    public int? CompanyId { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public string FullName { get; set; }
    public string Phone { get; set; }
    public string Mail { get; set; }

    public Company Company { get; set; }

    public ICollection<Order> Orders
    {
        get => LazyLoader.Load(this, ref _orders);
        set => _orders = value;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.