使用 EntityFramework 核心调用 MYSQL 存储过程并显示返回的数据 --
Use EntityFramework Core to call a MSSQL StoredProcedure and show the data returned --
好吧,这个问题不言自明。所以我不会不必要地扩展它。
这是我的 SP --
CREATE procedure [dbo].[sp_TotalSalesByCustomerID]
@customer_id nvarchar(50),
@varSale decimal OUTPUT
as
begin
begin try
if exists (
Select * from tempdb..sysobjects Where id =
object_id('tempdb.dbo.#temp')
)
DROP TABLE #temp
else
select * into #temp from Orders where customer_id=@customer_id
select @varSale=SUM(total_price) from #temp
end try
begin catch
select error_message(),
error_severity(),
error_number(),
error_state()
end catch
end
如您所见,SP 基本上 returns 只是一个计算值。它没有映射到我的应用程序中的任何实体 object/class。
我编写的后端 Web api 端的 C# 代码 --
[Route("TotalSalesByCustomerID")]
[HttpPost]
public JsonResult TotalSalesByCustomerID([FromBody]string customer_id)
{
try
{
//var salesT = (_appDbContext.Totals.FromSql("call sp_TotalSalesByCustomerID(@customer_id)", customer_id));
var salesT= _appDbContext.Database.ExecuteSqlCommand("sp_TotalSalesByCustomerID @p0", parameters: new[] { customer_id });
return Json(salesT);
}
catch(Exception exp)
{
return Json(exp.GetBaseException());
}
}
我不知道它返回的是什么 codswallop 垃圾。可能是因为它没有映射到实体 class 对象。我明白那个。可能某些 C# 代码也是错误的。我也不需要单独的 class。它只是一个值!如果我添加 class 我可能也需要进行迁移,这在我的场景中是不必要的。
仅供参考,这也是我的迁移构建器 class,我在其中添加了我的 sql 代码和 'Down' --
protected override void Up(MigrationBuilder migrationBuilder)
{
var sp = @"CREATE procedure [dbo].[sp_TotalSalesByCustomerID]
@customer_id nvarchar(50),
@varSale decimal OUTPUT
as
begin
begin try
if exists (
Select * from tempdb..sysobjects Where id = object_id('tempdb.dbo.#temp')
)
DROP TABLE #temp
else
select * into #temp from Orders where customer_id=@customer_id
select @varSale=SUM(total_price) from #temp
end try
begin catch
select error_message(),
error_severity(),
error_number(),
error_state()
end catch
end";
migrationBuilder.Sql(sp);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
var sp = @"CREATE procedure [dbo].[sp_TotalSalesByCustomerID]
@customer_id nvarchar(50),
@varSale decimal OUTPUT
as
begin
begin try
if exists (
Select * from tempdb..sysobjects Where id = object_id('tempdb.dbo.#temp')
)
DROP TABLE #temp
else
select * into #temp from Orders where customer_id=@customer_id
select @varSale=SUM(total_price) from #temp
end try
begin catch
select error_message(),
error_severity(),
error_number(),
error_state()
end catch
end";
migrationBuilder.Sql(sp);
}
我不知道该怎么办。当然,我在这里错过了一些不错的技巧或一些可以做到这一点的技巧。它是什么?我可以制作某种 virtual/abstract 实体,我可以在 EF Core 中使用它来获取我的数据,而不是创建表、向我的数据库添加迁移等吗?还有哪些选择?
我期待在这方面得到一些好的帮助。
谢谢大家,
您必须像下面的代码一样使用 return 值的输出参数 instdead。
var outputParam = new SqlParameter("outputParam", SqlDbType.Int) { Direction = ParameterDirection.Output };
await _databaseContext.Database.ExecuteSqlCommand($"EXEC {storedProcedureName} @param1, @outputParam output", new SqlParameter("param1"), outputParam);
要获取您需要在上面两行之后调用 outputParam.Value
的值。
ExecuteSqlCommand()
的 return 值是一个整数,表示受查询影响的记录数。
好吧,这个问题不言自明。所以我不会不必要地扩展它。 这是我的 SP --
CREATE procedure [dbo].[sp_TotalSalesByCustomerID]
@customer_id nvarchar(50),
@varSale decimal OUTPUT
as
begin
begin try
if exists (
Select * from tempdb..sysobjects Where id =
object_id('tempdb.dbo.#temp')
)
DROP TABLE #temp
else
select * into #temp from Orders where customer_id=@customer_id
select @varSale=SUM(total_price) from #temp
end try
begin catch
select error_message(),
error_severity(),
error_number(),
error_state()
end catch
end
如您所见,SP 基本上 returns 只是一个计算值。它没有映射到我的应用程序中的任何实体 object/class。 我编写的后端 Web api 端的 C# 代码 --
[Route("TotalSalesByCustomerID")]
[HttpPost]
public JsonResult TotalSalesByCustomerID([FromBody]string customer_id)
{
try
{
//var salesT = (_appDbContext.Totals.FromSql("call sp_TotalSalesByCustomerID(@customer_id)", customer_id));
var salesT= _appDbContext.Database.ExecuteSqlCommand("sp_TotalSalesByCustomerID @p0", parameters: new[] { customer_id });
return Json(salesT);
}
catch(Exception exp)
{
return Json(exp.GetBaseException());
}
}
我不知道它返回的是什么 codswallop 垃圾。可能是因为它没有映射到实体 class 对象。我明白那个。可能某些 C# 代码也是错误的。我也不需要单独的 class。它只是一个值!如果我添加 class 我可能也需要进行迁移,这在我的场景中是不必要的。 仅供参考,这也是我的迁移构建器 class,我在其中添加了我的 sql 代码和 'Down' --
protected override void Up(MigrationBuilder migrationBuilder)
{
var sp = @"CREATE procedure [dbo].[sp_TotalSalesByCustomerID]
@customer_id nvarchar(50),
@varSale decimal OUTPUT
as
begin
begin try
if exists (
Select * from tempdb..sysobjects Where id = object_id('tempdb.dbo.#temp')
)
DROP TABLE #temp
else
select * into #temp from Orders where customer_id=@customer_id
select @varSale=SUM(total_price) from #temp
end try
begin catch
select error_message(),
error_severity(),
error_number(),
error_state()
end catch
end";
migrationBuilder.Sql(sp);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
var sp = @"CREATE procedure [dbo].[sp_TotalSalesByCustomerID]
@customer_id nvarchar(50),
@varSale decimal OUTPUT
as
begin
begin try
if exists (
Select * from tempdb..sysobjects Where id = object_id('tempdb.dbo.#temp')
)
DROP TABLE #temp
else
select * into #temp from Orders where customer_id=@customer_id
select @varSale=SUM(total_price) from #temp
end try
begin catch
select error_message(),
error_severity(),
error_number(),
error_state()
end catch
end";
migrationBuilder.Sql(sp);
}
我不知道该怎么办。当然,我在这里错过了一些不错的技巧或一些可以做到这一点的技巧。它是什么?我可以制作某种 virtual/abstract 实体,我可以在 EF Core 中使用它来获取我的数据,而不是创建表、向我的数据库添加迁移等吗?还有哪些选择? 我期待在这方面得到一些好的帮助。 谢谢大家,
您必须像下面的代码一样使用 return 值的输出参数 instdead。
var outputParam = new SqlParameter("outputParam", SqlDbType.Int) { Direction = ParameterDirection.Output };
await _databaseContext.Database.ExecuteSqlCommand($"EXEC {storedProcedureName} @param1, @outputParam output", new SqlParameter("param1"), outputParam);
要获取您需要在上面两行之后调用 outputParam.Value
的值。
ExecuteSqlCommand()
的 return 值是一个整数,表示受查询影响的记录数。