简体   繁体   English

使用 EF Core 中返回的多个结果集映射存储过程

[英]Map stored procedure with multiple resultset returned in EF Core

I am currently migrating an already established website from old ASP.NET to ASP.NET Core 2.2.我目前正在将一个已经建立的网站从旧的 ASP.NET 迁移到 ASP.NET Core 2.2。 The database is a shared database across other platforms and is also established therefore I cannot just changed anything from it.该数据库是跨其他平台的共享数据库,也是建立的,因此我不能对其进行任何更改。

I came across to this problem when I had to call a stored procedure from EF Core where this stored procedure returns 3 result sets.当我不得不从 EF Core 调用存储过程时,我遇到了这个问题,该存储过程返回 3 个结果集。 The result set of the stored procedure is like this.存储过程的结果集是这样的。

Table 1表格1

ColumnName1 | ColumnName2 | SomeColumn | AndMoreColumns
------------+-------------+------------+---------------
Value1      | value 2     | value 3    | Value 4

Table 1表格1

Column1    | Column2
-----------+----------
value1     | value 2

Table 3表3

Column1
-------
Value1

Table 1 and table 2 are related to each other.表 1 和表 2 相互关联。 This table will always return 1 row on table 1 and table 2 can return multiple rows while table 3 is static.该表将始终返回表 1 上的 1 行,表 2 可以返回多行,而表 3 是静态的。

Now, my question is.现在,我的问题是。 How do I map this result set to an EF Core Query<T> model builder?如何将此结果集映射到 EF Core Query<T>模型构建器? I want to map it per column name since I want to make the column names more friendly.我想按列名映射它,因为我想让列名更友好。 An example that I had that did not work is like this.我有一个不起作用的例子是这样的。

modelBuilder.Query<MyModel>(a => 
{
    a.Property(b=>b.ModelId).HasColumnName("ColumnName1");
    a.Property(b=>b.ModelName).HasColumnName("ColumnName2");
    ...
});

but unfortunately, the above mapping did not work.但不幸的是,上述映射不起作用。 Especially for the 2nd table.特别是对于第二张桌子。

First at all, the result from stored procedure is not a table, I mean an user table.首先,存储过程的结果不是表,我的意思是用户表。

So, I think there are two possible solutions for your question:因此,我认为您的问题有两种可能的解决方案:

  1. Work with pure ADO.NET, execute data reader and cast the result to result models使用纯 ADO.NET,执行数据读取器并将结果转换为结果模型

  2. Use an external approach from EF Core使用 EF Core 的外部方法

For point 1, please read this question: EF Core query stored procedure map to types对于第 1 点,请阅读这个问题: EF Core 查询存储过程映射到类型

For point 2, please read this question: Working with multiple resultset in .net core对于第 2 点,请阅读这个问题: 在 .net core 中使用多个结果集

Also you can read this issue: Support multiple resultsets #8127您也可以阅读此问题:支持多个结果集 #8127

Let me know if this answer is useful.让我知道这个答案是否有用。

You might want this post until EF Core implement similar function:在 EF Core 实现类似功能之前,您可能想要这篇文章:

https://www.danylkoweb.com/Blog/aspnet-core-with-entity-framework-core-returning-multiple-resultsets-OL https://www.danylkoweb.com/Blog/aspnet-core-with-entity-framework-core-returning-multiple-resultsets-OL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM