在客户端未获取虚拟集合数据

huangapple go评论62阅读模式
英文:

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 =&gt; x.CompanyLicenseTypeDetails)
                                .AsNoTracking()
                                .Where(x =&gt; 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&lt;CompanyLicenseTypeDetail&gt; CompanyLicenseTypeDetails { get; } = new List&lt;CompanyLicenseTypeDetail&gt;();
}

And this my API call on client side

var result = await _http.Client.GetFromJsonAsync&lt;ServiceResponse&lt;CompanyMaster&gt;&gt;($&quot;api/Company/Company/{companyId}&quot;);

答案1

得分: 1

你需要将 CompanyLicenseTypeDetails 属性中添加 set;

public virtual ICollection&lt;CompanyLicenseTypeDetail&gt; CompanyLicenseTypeDetails { get; set; } = new List&lt;CompanyLicenseTypeDetail&gt;();

然后你就可以正常获取 CompanyLicenseTypeDetail 列表了。

英文:

You need to add set; to your CompanyLicenseTypeDetails property

public  virtual ICollection&lt;CompanyLicenseTypeDetail&gt; CompanyLicenseTypeDetails { get; set;} = new List&lt;CompanyLicenseTypeDetail&gt;();

Then you can get list of CompanyLicenseTypeDetail normally.

huangapple
  • 本文由 发表于 2023年3月31日 19:17:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定