[英]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.User
和Session.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
不起作用?
2.1.300-preview2-008533
2.1.300-preview2-008533
netcoreapp2.1
netcoreapp2.1
2.1.0-preview2-final
2.1.0-preview2-final
Steps To Configure Lazy Loading with Proxies in Asp.net Core 2.1 在Asp.net Core 2.1中使用代理配置延迟加载的步骤
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLazyLoadingProxies()
.UseSqlServer(myConnectionString);
Or when using AddDbContext: 或者在使用AddDbContext时:
.AddDbContext<BloggingContext>(
b => b.UseLazyLoadingProxies()
.UseSqlServer(myConnectionString));
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.