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

时间:2018-05-04 17:33:15

标签: c# entity-framework asp.net-core .net-core entity-framework-core

我有以下型号:

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

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

我正在尝试使用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();

我正在使用context.All<Session>查询会话。但是,Session.UserSession.User.Organization默认为null。要加载它们,我必须执行context.All<Session>().Include(s => s.User).Include(s => s.User.Organization)之类的操作。我怎么能避免这种情况?为什么UseLazyLoadingProxies无效?

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

3 个答案:

答案 0 :(得分:8)

在Asp.net Core 2.1中配置代理的延迟加载的步骤

  1. 安装Microsoft.EntityFrameworkCore.Proxies软件包
  2. 启用LazyLoadingProxies 您可以通过调用UseLazyLoadingProxies来启用它:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .UseLazyLoadingProxies()
        .UseSqlServer(myConnectionString);

或者在使用AddDbContext时:

.AddDbContext<BloggingContext>(
b => b.UseLazyLoadingProxies()
      .UseSqlServer(myConnectionString));
  1. 然后,EF Core将为可以覆盖的任何导航属性启用延迟加载,也就是说,它必须是虚拟的。

答案 1 :(得分:7)

您可以尝试在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
}

顺便说一句,您已经可以将应用程序包更新为纯2.1.0(非最终版或RC版)。您的配置可能无法正常工作的原因之一是组件的不稳定版本。

注意:Microsoft.EntityFrameworkCore.Proxies.dll是从nuget独立于EFCore

安装的

答案 2 :(得分:0)

我通过在服务中设置JSON序列化来解决此问题,例如: https://stackoverflow.com/a/49350457/4178475