.net core 使用代理的实体框架核心延迟加载不起作用(SQLite)
使用延迟加载时,我希望未加载子导航属性 但我无法避免在序列化为json时加载子对象并导致循环引用.net core 使用代理的实体框架核心延迟加载不起作用(SQLite),.net-core,entity-framework-core,.net Core,Entity Framework Core,使用延迟加载时,我希望未加载子导航属性 但我无法避免在序列化为json时加载子对象并导致循环引用 DbContext: public class SampleDbContext : DbContext { public SampleDbContext(DbContextOptions<SampleDbContext> options: base(options) { } protected override void OnConfiguring(DbCont
DbContext
:
public class SampleDbContext : DbContext
{
public SampleDbContext(DbContextOptions<SampleDbContext> options: base(options)
{ }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLazyLoadingProxies();
}
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Children { get; set; }
}
公共类SampleDbContext:DbContext
{
公共SampleDbContext(DbContextOptions:base(选项)
{ }
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
optionsBuilder.UseLazyLoadingProxies();
}
公共数据库集父项{get;set;}
公共DbSet子项{get;set;}
}
模型类:
public class Child
{
public int Id { get; set; }
[ForeignKey("Parent")]
public int ParentId { get; set; }
public string Name { get; set; }
public virtual Parent parent { get; set; }
}
public class Parent
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Child> children { get; set; }
}
公共类子类
{
公共int Id{get;set;}
[外键(“母公司”)]
public int ParentId{get;set;}
公共字符串名称{get;set;}
公共虚拟父级{get;set;}
}
公共类父类
{
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection子项{get;set;}
}
启动:
services.AddDbContext<Models.SampleDbContext>(options =>options.UseLazyLoadingProxies().UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext(options=>options.UseLazyLoadingProxies().UseSqlite(Configuration.GetConnectionString(“DefaultConnection”));
控制器:
[HttpGet]
public async Task<List<Models.Parent>> Get()
{
var sample = _cont.Parents.ToList();
return sample;
}
[HttpGet]
公共异步任务Get()
{
var sample=_cont.Parents.ToList();
返回样品;
}
使用:
- EF核心版本:2.2.1
- 数据库提供程序:Microsoft.EntityFrameworkCore.Sqlite 2.2.1
- 操作系统:Win10
- IDE:Visual Studio 2017
谢谢,但是我们怎么能说延迟加载有效呢?
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
...
}