【发布时间】:2021-09-09 12:13:38
【问题描述】:
我正在探索 EF Core 延迟加载,但很难通过 ASP.NET Web API 返回结果。请在下面查看我的 API 调用。
// 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());
}
是的。我已经添加了包Microsoft.EntityFrameworkCore.Proxies
我在我的Startup.cs 中添加了UseLazyLoadingProxies
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);
}
);
我使用Pomelo.EntityFrameworkCore.MySql 作为数据库提供者。
当我从 swagger 调用 Web API 时,它会在后台进行大量数据库调用。这是我在终端中看到的。它一直在打这些电话。我不得不停止应用程序来停止这些调用。我不确定我在这里做错了什么。还有其他人面临这个问题吗?
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
当我使用return await Task.FromResult(new Supplier()); 时,它返回一个空的供应商。
谢谢 好奇的驱动器
【问题讨论】:
-
尝试手动或使用映射器(如 AutoMapper)将实体映射到普通对象,以在将结果返回给客户端之前强制获取关系。即使用 DTO
-
无关:你可以简单的在异步函数中返回结果,或者
return Task.FromResult(val),你不需要await Task.FromResult(val) -
有趣...我尝试访问供应商的属性之一(即公司名称)并将其返回到另一个对象中并且它有效。似乎它无法返回虚拟财产本身。在返回之前我必须将它复制到一些东西中?
-
是的,在它尝试从数据库中查询数据之前,您必须尝试访问它。
-
嗯....那是一个曲线球。无论如何,感谢您的评论。它现在正在工作。
标签: c# .net asp.net-core entity-framework-core asp.net-core-webapi