【发布时间】:2019-12-03 19:52:07
【问题描述】:
首先,我阅读了属于这个问题的所有答案,但到目前为止没有任何帮助,所以在将其标记为重复之前阅读一遍。
我创建了一个名为 ProjectMaster 的实体,它有一个名为 ClientMaster 的 虚拟 属性。 当我只在 mvc 中工作时它正在加载数据。但是现在我已经迁移到 CORE 并且这里没有加载。 谷歌搜索后,我知道使用延迟加载加载虚拟属性需要实现两件事。
- 安装
Microsoft.EntityFrameworkCore.Proxies - 在启动中的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());
}
这是我迄今为止所尝试的所有努力。
这里需要注意的是,该实体存在于类库项目中,并且所有必要的包都已加载到其中。
如果你有的话,给我一些有用的建议。
【问题讨论】:
标签: c# asp.net-core .net-core asp.net-core-mvc entity-framework-core