C# 在ASP.NET核心中延迟加载后未加载虚拟属性
首先,我读了所有属于这个问题的答案,但到目前为止没有任何帮助,所以在标记为重复之前,先读一次 我创建了一个名为ProjectMaster的实体,该实体的虚拟属性名为ClientMaster。当我只在mvc中工作时,它正在加载数据。但现在我已经迁移到CORE,这里没有加载。 在谷歌搜索之后,我知道有两件事需要实现,即使用延迟加载加载虚拟属性C# 在ASP.NET核心中延迟加载后未加载虚拟属性,c#,asp.net-core,.net-core,asp.net-core-mvc,entity-framework-core,C#,Asp.net Core,.net Core,Asp.net Core Mvc,Entity Framework Core,首先,我读了所有属于这个问题的答案,但到目前为止没有任何帮助,所以在标记为重复之前,先读一次 我创建了一个名为ProjectMaster的实体,该实体的虚拟属性名为ClientMaster。当我只在mvc中工作时,它正在加载数据。但现在我已经迁移到CORE,这里没有加载。 在谷歌搜索之后,我知道有两件事需要实现,即使用延迟加载加载虚拟属性 安装Microsoft.EntityFrameworkCore.Proxies 在启动内部的ConfigureServices中调用UseLazyLoadi
- 安装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());
}
[表格(“ProjectMaster”)]
公共部分类ProjectMaster
{
[关键]
公共Guid项目ID{get;set;}
[必需]
[长度(500)]
公共字符串ProjectName{get;set;}
[必需]
[长度(500)]
公共字符串项目代码{get;set;}
公共Guid客户端ID{get;set;}
通过{get;set;}创建的公共Guid
公共虚拟客户端主机客户端主机{get;set;}
}
[表(“客户主机”)]
公共部分类ClientMaster
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共客户端主机()
{
ProjectMasters=newhashset();
}
[关键]
公共Guid客户端ID{get;set;}
[必需]
[长度(100)]
公共字符串ClientName{get;set;}
通过{get;set;}创建的公共Guid
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection ProjectMasters{get;set;}
}
启动:
[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());
}
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
服务。AddEntityFrameworkProxies();
services.AddDbContextPool(选项=>
{
options.UseSqlServer(Configuration.GetConnectionString(“amcConn”);
选项。使用LazyLoadingProxies(true);
});
//services.AddDbContextPool(options=>options.UseSqlServer(Configuration.GetConnectionString(“amcConn”));
//services.AddDbContextPool(options=>options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString(“amcConn”));
services.AddSession();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
控制器:
[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());
}
public apireult getallProject()
{
尝试
{
AMCContext_contect=新的AMCContext();
返回新结果
(新的ApiResultCode(ApiResultType.Success),\u contect.ProjectMasters.ToList());
}
这里是我迄今为止所做的所有努力
这里需要注意的一点是,这个实体存在于类库项目中,所有必要的包都已加载到其中
如果有,请给我一些有用的建议。我当前使用的版本
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="2.2.4" />
样本实体
public class Post : BaseEntity
{
public string Title { get; set; }
public string ShortDescription { get; set; }
public string Content { get; set; }
public PostStatus PostStatus { get; set; }
public int Views { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual Media Medias { get; set; }
}
public class Comment : BaseEntity
{
public string Content { get; set; }
public virtual Comment ParentComment { get; set; }
public virtual Post Post { get; set; }
public virtual User? User { get; set; }
public CommentStatus CommentStatus { get; set; }
}
当您使用延迟加载时,它会在您尝试访问时加载值
您可以使用foreach循环来实现此目的,如图所示
下面:它将通过Include工作,但这将添加渴望加载的方法。我想通过延迟加载来实现这一点。我已经尝试了你建议的所有方法。嗯,另一件事我可以建议它删除obj并调试文件夹,然后清理。重新生成并运行你的项目。我已经尝试了你建议的方法,但也不起作用。我不明白为什么会这样t简单的一步是不会导致我。