[英]EF Core Lazy-Loading - Navigation properties fail with “The type or namespace name 'ProductProxy' does not exist in the namespace 'Castle.Proxies'”
使用 EF Core 2.2.3,我根據文檔https://docs.microsoft.com/en-us/ef/core/querying/related-data#lazy-loading設置了延遲加載
啟動:
public void ConfigureServices(IServiceCollection services)
{
// snipped other stuff
services.AddScoped<IProductRepository, ProductRepository>();
services.AddDbContext<ProductsDbContext>(options => {
options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("ProductsConnection"));
});
}
示例實體:
public class Product: IEntity
{
public Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ProductIndustry> ProductIndustries { get; set; }
}
產品存儲方法:
public ServiceResponse<TEntity> GetByIdAsync(Guid id)
{
try
{
var entity = DbContext.Set<TEntity>()
.AsNoTracking()
.FirstOrDefault(e => e.Id == id);
return ServiceResponseFactory.SuccessfulResponse(entity);
}
catch (Exception ex)
{
LogService.LogException(ex);
return ServiceResponseFactory.FailedResponse<TEntity>();
}
}
如果我在調試時檢查實體,我的導航屬性會顯示以下錯誤:
((Castle.Proxies.ProductProxy)productResponse.Content).ProductIndustries' threw an exception of type 'System.InvalidOperationException
如果我單擊屬性旁邊的重新加載圖標,錯誤將變為:
error CS0234: The type or namespace name 'ProductProxy' does not exist in the namespace 'Castle.Proxies' (are you missing an assembly reference?)
我正在對現有項目進行延遲加載改造,並且我已經刪除了數據庫層上的所有異步代碼,並且我也嘗試過 remove.AsNoTracking() 但這並沒有對其進行排序。
這方面的文檔非常簡單,所以我不確定我錯過了什么。
在您的實體 class 中,在虛擬 ProductIndustriesProperty 的聲明上方,輸入:
[ForeignKey("ParentId")]
在引號中輸入 ProductIndustry 上的屬性名稱,這是您父母的關鍵。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.