英文:
Not getting virtual collection data on the client side
问题
I have a class in my ASP.NET Core Web API. I am using EF Core to load the data.
var dataOutPut = await _context.CompanyMasters
                                    .Include(x => x.CompanyLicenseTypeDetails)
                                    .AsNoTracking()
                                    .Where(x => x.CompanyId == 45)
                                    .FirstOrDefaultAsync();
Data is loading without any issue on the server. Getting company details and associated CompanyLicenseTypeDetail collection. I confirmed with swagger and postman.
But when I tried to get this data in a Blazor wasm client, I am getting company detail but I'm not getting the list of CompanyLicenseTypeDetail. It's showing empty on the client.
This is my class:
public partial class CompanyMaster
{
    public decimal CompanyId { get; set; }
    public string CompanyName { get; set; } = null!;
    public virtual ICollection<CompanyLicenseTypeDetail> CompanyLicenseTypeDetails { get; } = new List<CompanyLicenseTypeDetail>();
}
And this my API call on the client side
var result = await _http.Client.GetFromJsonAsync<ServiceResponse<CompanyMaster>>($"api/Company/Company/{companyId}");
英文:
I have a class in my ASP.NET Core Web API. I am using EF Core to load the data.
var dataOutPut =  await _context.CompanyMasters
                                .Include(x => x.CompanyLicenseTypeDetails)
                                .AsNoTracking()
                                .Where(x => x.CompanyId == 45)
                                .FirstOrDefaultAsync();
Data is loading without any issue on the server. Getting company details and associated CompanyLicenseTypeDetail collection. I confirmed with swagger and postman.
But when I tried to get this data in a Blazor wasm client, I am getting company detail but I'm not getting list of CompanyLicenseTypeDetail. It's showing empty on the client.
This is my class:
public partial class CompanyMaster
{
    public decimal CompanyId { get; set; }
    public string CompanyName { get; set; } = null!;
    public  virtual ICollection<CompanyLicenseTypeDetail> CompanyLicenseTypeDetails { get; } = new List<CompanyLicenseTypeDetail>();
}
And this my API call on client side
var result = await _http.Client.GetFromJsonAsync<ServiceResponse<CompanyMaster>>($"api/Company/Company/{companyId}");
答案1
得分: 1
你需要将 CompanyLicenseTypeDetails 属性中添加 set;:
public virtual ICollection<CompanyLicenseTypeDetail> CompanyLicenseTypeDetails { get; set; } = new List<CompanyLicenseTypeDetail>();
然后你就可以正常获取 CompanyLicenseTypeDetail 列表了。
英文:
You need to add set; to your CompanyLicenseTypeDetails property
public  virtual ICollection<CompanyLicenseTypeDetail> CompanyLicenseTypeDetails { get; set;} = new List<CompanyLicenseTypeDetail>();
Then you can get  list of CompanyLicenseTypeDetail normally.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论