Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net core 使用代理的实体框架核心延迟加载不起作用(SQLite)_.net Core_Entity Framework Core - Fatal编程技术网

.net core 使用代理的实体框架核心延迟加载不起作用(SQLite)

.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

使用延迟加载时,我希望未加载子导航属性 但我无法避免在序列化为json时加载子对象并导致循环引用

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
来自:

由于EF Core将自动修复导航属性,因此您可以在对象图中以循环结束

如果您使用的是ASP.NET核心,则可以将Json.NET配置为忽略在对象图中找到的循环


谢谢,但是我们怎么能说延迟加载有效呢?
public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddMvc()
        .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );

    ...
}