How to make lazy-loading work with EF Core 2.1.0 and proxies

前端 未结 3 611
北海茫月
北海茫月 2020-12-29 07:48

I have the following models:

public class Session
{
    public int SessionID { get; set; }
    public int UserID { get; set; }

    public virtual User User          


        
3条回答
  •  悲哀的现实
    2020-12-29 08:18

    Steps To Configure Lazy Loading with Proxies in Asp.net Core 2.1

    1. Install Microsoft.EntityFrameworkCore.Proxies package
    2. Enable LazyLoadingProxies You can enable it with a call to UseLazyLoadingProxies:
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseLazyLoadingProxies()
            .UseSqlServer(myConnectionString);
    

    Or when using AddDbContext:

    .AddDbContext(
    b => b.UseLazyLoadingProxies()
          .UseSqlServer(myConnectionString));
    
    1. EF Core will then enable lazy loading for any navigation property that can be overridden--that is, it must be virtual.

提交回复
热议问题