简体   繁体   English

如何使用EF Core 2.1.0和代理进行延迟加载

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

I have the following models: 我有以下型号:

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

    public virtual User User { get; set; }
}

public class User
{
    public int UserID { get; set; }
    public int OrganizationID { get; set; }

    public virtual ICollection<Session> Sessions { get; set; }
    public virtual Organization Organization { get; set; }
}

public class Organization
{
    public int OrganizationID { get; set; }

    public virtual ICollection<User> Users { get; set; }
}

that are registered in DbContext as: DbContext中注册为:

modelBuilder.Entity<Session>(entity =>
{
    entity.ToTable("sessions");

    entity.Property(e => e.SessionID).HasColumnName("id");
    entity.Property(e => e.UserID).HasColumnName("user_id");

    entity.HasOne(e => e.User)
        .WithMany(e => e.Sessions)
        .HasForeignKey(e => e.UserID);
}

modelBuilder.Entity<User>(entity =>
{
    entity.ToTable("users");

    entity.Property(e => e.UserID).HasColumnName("id");
    entity.Property(e => e.OrganizationID).HasColumnName("organization_id");

    entity.HasOne(e => e.Organization)
        .WithMany(e => e.Users)
        .HasForeignKey(e => e.OrganizationID);
}

modelBuilder.Entity<Organization>(entity =>
{
    entity.ToTable("organizations");

    entity.Property(e => e.OrganizationID).HasColumnName("id");
}

I'm trying to use lazy loading with Microsoft.EntityFrameworkCore.Proxies as described here : 我试图使用延迟加载与Microsoft.EntityFrameworkCore.Proxies描述在这里

builder.Register(c =>
{
    var optionsBuilder = new DbContextOptionsBuilder<Context>();
    optionsBuilder
        .UseLazyLoadingProxies()
        /* more options */
        ;

    var opts = optionsBuilder.Options;

    return new Context(opts);
}).As<DbContext>().InstancePerLifetimeScope();

I'm querying sessions using context.All<Session> . 我正在使用context.All<Session>查询会话。 However, Session.User and Session.User.Organization are null by default. 但是,默认情况下, Session.UserSession.User.Organization为null。 To load them I have to do something like context.All<Session>().Include(s => s.User).Include(s => s.User.Organization) . 要加载它们,我必须做类似context.All<Session>().Include(s => s.User).Include(s => s.User.Organization) How can I avoid that? 我怎么能避免这种情况? Why doesn't UseLazyLoadingProxies work? 为什么UseLazyLoadingProxies不起作用?


  • .NET Core version: 2.1.300-preview2-008533 .NET核心版本: 2.1.300-preview2-008533
  • Target: netcoreapp2.1 目标: netcoreapp2.1
  • EF Core (and Proxies) version: 2.1.0-preview2-final EF Core(和Proxies)版本: 2.1.0-preview2-final

Steps To Configure Lazy Loading with Proxies in Asp.net Core 2.1 在Asp.net Core 2.1中使用代理配置延迟加载的步骤

  1. Install Microsoft.EntityFrameworkCore.Proxies package 安装Microsoft.EntityFrameworkCore.Proxies包
  2. Enable LazyLoadingProxies You can enable it with a call to UseLazyLoadingProxies: 启用LazyLoadingProxies您可以通过调用UseLazyLoadingProxies来启用它:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .UseLazyLoadingProxies()
        .UseSqlServer(myConnectionString);

Or when using AddDbContext: 或者在使用AddDbContext时:

.AddDbContext<BloggingContext>(
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. 然后,EF Core将为可以覆盖的任何导航属性启用延迟加载 - 也就是说,它必须是虚拟的。

You can try to configure proxies in Startup.cs like 您可以尝试在Startup.cs配置代理

public void ConfigureServices(IServiceCollection services)
{
    #region Database configuration

    // Database configuration
    services.AddDbContext<DbContext>(options =>
        options.UseLazyLoadingProxies()
            .UseSqlServer(Configuration.GetConnectionString("MyConnectionString")));

    #endregion Database configuration
}

And by the way you can already update you app packages to pure 2.1.0 (not final or RC). 顺便说一句,您已经可以将应用程序包更新为纯2.1.0(非最终版或RC版)。 One of the reasons why your configuration may not work is an unstable version of components. 配置可能不起作用的原因之一是组件的不稳定版本。

NB: Microsoft.EntityFrameworkCore.Proxies.dll is installed from nuget independently from EFCore 注意: Microsoft.EntityFrameworkCore.Proxies.dll是从nuget独立于EFCore安装的

我通过在服务中设置JSON序列化来解决这个问题,就像这个答案https://stackoverflow.com/a/49350457/4178475

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core 延迟加载 - 导航属性失败,并显示“命名空间 'Castle.Proxies' 中不存在类型或命名空间名称 'ProductProxy'” - EF Core Lazy-Loading - Navigation properties fail with “The type or namespace name 'ProductProxy' does not exist in the namespace 'Castle.Proxies'” EF Core:分离的延迟加载导航实体。 为什么? - EF Core: detached lazy-loading navigation entity. Why? EF Core:分离的延迟加载导航属性 - EF Core: Detached lazy-loading navigation property 在 blazor 中使用 EfCore 延迟加载代理 - Using EfCore lazy-loading proxies with blazor EF 4 - 没有代理的延迟加载 - EF 4 - Lazy Loading Without Proxies ASP.NET Core Web API 无法返回使用 EF Core 延迟加载提取的结果 - ASP.NET Core Web API can not return results extracted using EF Core lazy-loading 具有逻辑和使用导航属性的 EF Core NotMapped Getter - 分离实体不支持延迟加载 - EF Core NotMapped Getter with logic and using navigation properties - lazy-loading is not supported for detached entities EF Core JsonIgnore 不适用于延迟加载 - EF Core JsonIgnore does not work with Lazy Loading EF 延迟加载:将项目添加到导航属性而不加载它 - EF Lazy-Loading: Add item to navigation property without loading it 如何在 EF Core 中对 DbQuery 使用延迟加载? - How to use Lazy Loading for DbQuery in EF Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM