I am exploring EF Core lazy-loading, but struggling to return the results over ASP.NET Web API. Please check my API call below.
// GET: api/Supplier
[HttpGet("/GetSupplierUsingLazyLoading")]
public async Task<ActionResult<Supplier>> GetSupplierUsingLazyLoading()
{
var products = _context.Products.ToList();
var supplier = products.Last().Supplier;
return await Task.FromResult(supplier);
//return await Task.FromResult(new Supplier());
}
Yes. I have added the package Microsoft.EntityFrameworkCore.Proxies
And I have added UseLazyLoadingProxies in my Startup.cs
services.AddDbContext<NorthwindContext>(
options =>
{
options.UseMySql(Configuration.GetConnectionString("Northwind_MySQL"), Microsoft.EntityFrameworkCore.ServerVersion.Parse("8.0.23-mysql"));
options.UseLazyLoadingProxies();
options.LogTo(Console.WriteLine, LogLevel.Information);
}
);
I am using Pomelo.EntityFrameworkCore.MySql as database provider.
When I call the Web API from swagger then it makes lot of database calls behind the scenes. This is what I see in the terminal. It keeps on making these call. I had to stop the application to stop these calls. I am not sure what I am doing wrong here. Is anyone else facing this issue?
info: 6/26/2021 08:01:55.859 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (1ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT `o`.`OrderDetailsID`, `o`.`Discount`, `o`.`OrderID`, `o`.`ProductID`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `orderdetails` AS `o`
WHERE `o`.`OrderID` = @__p_0
info: 6/26/2021 08:01:55.863 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (0ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT `o`.`OrderDetailsID`, `o`.`Discount`, `o`.`OrderID`, `o`.`ProductID`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `orderdetails` AS `o`
WHERE `o`.`OrderID` = @__p_0
info: 6/26/2021 08:01:55.867 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (0ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT `o`.`OrderDetailsID`, `o`.`Discount`, `o`.`OrderID`, `o`.`ProductID`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `orderdetails` AS `o`
WHERE `o`.`OrderID` = @__p_0
info: 6/26/2021 08:01:55.870 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (0ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT `o`.`OrderDetailsID`, `o`.`Discount`, `o`.`OrderID`, `o`.`ProductID`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `orderdetails` AS `o`
WHERE `o`.`OrderID` = @__p_0
info: 6/26/2021 08:01:55.876 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (1ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT `o`.`OrderDetailsID`, `o`.`Discount`, `o`.`OrderID`, `o`.`ProductID`, `o`.`Quantity`, `o`.`UnitPrice`
FROM `orderdetails` AS `o`
WHERE `o`.`OrderID` = @__p_0
When I use return await Task.FromResult(new Supplier()); then it returns an empty supplier.
Thanks Curious Drive

return Task.FromResult(val), you don't need toawait Task.FromResult(val)