首先,我阅读了所有与此问题相关的答案,但迄今为止没有任何帮助,所以请在标记为重复之前仔细阅读一次。
我创建了一个名为
我创建了一个名为
ProjectMaster
的实体,它具有一个名为ClientMaster
的虚拟属性。 它在我使用MVC时加载数据。 但现在我已经迁移到CORE,在这里它没有被加载。
经过谷歌搜索,我知道需要实现两件事才能使用延迟加载加载虚拟属性:
- 安装
Microsoft.EntityFrameworkCore.Proxies
- 在startup中的ConfigureServices调用
UseLazyLoadingProxies()
服务
[Table("ProjectMaster")]
public partial class ProjectMaster
{
[Key]
public Guid ProjectId { get; set; }
[Required]
[StringLength(500)]
public string ProjectName { get; set; }
[Required]
[StringLength(500)]
public string ProjectCode { get; set; }
public Guid ClientId { get; set; }
public Guid CreatedBy { get; set; }
public virtual ClientMaster ClientMaster { get; set; }
}
[Table("ClientMaster")]
public partial class ClientMaster
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public ClientMaster()
{
ProjectMasters = new HashSet<ProjectMaster>();
}
[Key]
public Guid ClientId { get; set; }
[Required]
[StringLength(100)]
public string ClientName { get; set; }
public Guid CreatedBy { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ProjectMaster> ProjectMasters { get; set; }
}
初创企业:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddEntityFrameworkProxies();
services.AddDbContextPool<ApplicationDBContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("amcConn"));
options.UseLazyLoadingProxies(true);
});
//services.AddDbContextPool<ApplicationDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("amcConn")));
// services.AddDbContextPool<ApplicationDBContext>(options => options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("amcConn")));
services.AddSession();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
控制器:
public ApiResult<List<ProjectMaster>> getallProject()
{
try
{
AMCContext _contect = new AMCContext();
return new ApiResult<List<ProjectMaster>>
(new ApiResultCode(ApiResultType.Success), _contect.ProjectMasters.ToList());
}
这是我迄今为止所尝试的所有努力。
需要注意的一点是,此实体位于类库项目中,并已加载所有必要的包。
如果你有一些有用的建议,请告诉我。