我做了以下几件事,设法避开了这个问题。
.Include(s => s.Pages)
从这里开始,
var surveys = await _context.Surveys.Include(s => s.Pages).SingleOrDefaultAsync(s => s.Syear == syear && s.Quarter == quarter);
virtual
关键字添加到所有引用的类。
public partial class Pages
{
public Pages()
{
Elements = new HashSet<Elements>();
}
public string Id { get; set; }
public long Number { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int? PageFlowId { get; set; }
public bool NamedPage { get; set; }
public bool? IsFirst { get; set; }
public bool? IsLast { get; set; }
public long? SurveyQuarter { get; set; }
public long? SurveySyear { get; set; }
public virtual PagesFlows PageFlow { get; set; }
public virtual Surveys Survey { get; set; }
public virtual ICollection<Elements> Elements { get; set; }
}
然后我安装了
Microsoft.EntityFrameworkCore.Proxies
如前所述的NuGet包装
here
并在中启用了延迟加载代理
Sturtup.cs
在里面
ConfigureServices
services.AddDbContext<MyDBContext>
(options => options.UseLazyLoadingProxies().UseSqlServer(connection));