如何使延迟加载与EF Core 2.1.0和代理一起工作

我有以下模型:


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

}

我试图使用延迟加载与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.User和Session.User.Organization默认情况下为空。要加载它们,我必须做类似的事情context.All<Session>().Include(s => s.User).Include(s => s.User.Organization)。我该如何避免呢?为什么不起作用UseLazyLoadingProxies?

  • .NET Core版本: 2.1.300-preview2-008533

  • 目标: netcoreapp2.1

  • EF Core(和代理)版本: 2.1.0-preview2-final


慕虎7371278
浏览 108回答 2
2回答

猛跑小猪

您可以尝试Startup.cs像这样配置代理public void ConfigureServices(IServiceCollection services){&nbsp; &nbsp; #region Database configuration&nbsp; &nbsp; // Database configuration&nbsp; &nbsp; services.AddDbContext<DbContext>(options =>&nbsp; &nbsp; &nbsp; &nbsp; options.UseLazyLoadingProxies()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .UseSqlServer(Configuration.GetConnectionString("MyConnectionString")));&nbsp; &nbsp; #endregion Database configuration}顺便说一下,您已经可以将应用程序包更新为纯2.1.0(不是最终版或RC)。您的配置可能无法正常工作的原因之一是组件的版本不稳定。注意:Microsoft.EntityFrameworkCore.Proxies.dll是从nuget上独立于EFCore安装的

森林海

在 Asp.net Core 2.1 中使用代理配置延迟加载的步骤安装 Microsoft.EntityFrameworkCore.Proxies 包启用LazyLoadingProxies可以通过调用UseLazyLoadingProxies来启用它:protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)&nbsp; &nbsp; => optionsBuilder&nbsp; &nbsp; &nbsp; &nbsp; .UseLazyLoadingProxies()&nbsp; &nbsp; &nbsp; &nbsp; .UseSqlServer(myConnectionString);或使用AddDbContext时:.AddDbContext<BloggingContext>(b => b.UseLazyLoadingProxies()&nbsp; &nbsp; &nbsp; .UseSqlServer(myConnectionString));然后,EF Core将为可以被覆盖的任何导航属性启用延迟加载,也就是说,它必须是虚拟的。
打开App,查看更多内容
随时随地看视频慕课网APP