简体   繁体   English

如何使用 FromSqlRaw Entity Framework Core 3.1 从存储过程返回多个 SELECT 集

[英]How to return multiple SELECT sets from a stored procedure using FromSqlRaw Entity Framework Core 3.1

I am currently having an issue with my returning values from the DB, I am using a SQL Server stored procedure and ASP.NET Core 3.1 to retrieve several SELECT sets.我目前在从数据库返回值时遇到问题,我正在使用 SQL Server 存储过程和 ASP.NET Core 3.1 来检索多个 SELECT 集。

Here's my stored procedure:这是我的存储过程:

ALTER PROCEDURE [dbo].[MyProc]
    @BusinessModelID int
AS
BEGIN
    SELECT Id, Title 
    FROM [dbo].[Channels] 
    WHERE BusinessModelId = @BusinessModelID;

    SELECT Id, Title 
    FROM [dbo].[Problems] 
    WHERE BusinessModelId = @BusinessModelID;

    SELECT Id, Title 
    FROM [dbo].[CostStructures] 
    WHERE BusinessModelId = @BusinessModelID;

    SELECT Id, Title 
    FROM [dbo].[CustomerSegments] 
    WHERE BusinessModelId = @BusinessModelID;

    SELECT Id, Title 
    FROM [dbo].[KeyMetrics] 
    WHERE BusinessModelId = @BusinessModelID;

    SELECT Id, Title 
    FROM [dbo].[RevenueStreams] 
    WHERE BusinessModelId = @BusinessModelID;

    SELECT Id, Title 
    FROM [dbo].[Solutions] 
    WHERE BusinessModelId = @BusinessModelID;

    SELECT Id, Title 
    FROM [dbo].[Unfairs] 
    WHERE BusinessModelId = @BusinessModelID;

    SELECT Id, Title 
    FROM [dbo].[Values] 
    WHERE BusinessModelId = @BusinessModelID;
END

And here's my C# code:这是我的 C# 代码:

var modelList = _dataContext.Set<BusinessToGet>()
                        .FromSqlRaw($"EXECUTE MyProc @BusinessModelID={businessModelId}")
                        .ToList();

The problem is that it should return several multiple sets but it is only returning one the Channels.问题是它应该返回多个多组,但它只返回一个通道。 What am I doing wrong?我究竟做错了什么?

Thanks in advance.提前致谢。

Update更新

In this case, there was duplicate primary keys across tables.在这种情况下,表中存在重复的主键。 Even though SQL below worked well in SQL-server, EF Core returned a lot of duplicate instances due to the fact that when EF Core sees an instance with Id = 1, it uses that for all sub-sequent instances with Id = 1.尽管下面的 SQL 在 SQL-server 中运行良好,但 EF Core 返回了大量重复实例,因为当 EF Core 看到一个 Id = 1 的实例时,它会将它用于所有 Id = 1 的后续实例。

A work-around is to use a keyless DTO for FromSqlRaw queries.解决方法是对FromSqlRaw查询使用无密钥 DTO。

[Keyless]
public class BlogPostsCount
{
Id, 
    public int Id { get; set; }
    public string Title { get; set; }
}

https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=data-annotations https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=data-annotations


Using UNION should solve this issue.使用UNION应该可以解决这个问题。 If datatypes varies between tables, you may need to cast column values as well in order to make a uniform result.如果表之间的数据类型不同,您可能还需要转换列值以获得统一的结果。 If you have duplicates across tables, and needs all, use UNION ALL .如果跨表有重复项,并且需要全部,请使用UNION ALL

SELECT Id, Title FROM [dbo].[Channels] WHERE BusinessModelId = @BusinessModelID
UNION
SELECT Id,Title FROM [dbo].[Problems] WHERE BusinessModelId = @BusinessModelID
UNION
SELECT Id, Title FROM [dbo].[CostStructures] WHERE BusinessModelId = @BusinessModelID
UNION
SELECT Id, Title FROM [dbo].[CustomerSegments] WHERE BusinessModelId = @BusinessModelID
UNION
SELECT Id, Title FROM [dbo].[KeyMetrics] WHERE BusinessModelId = @BusinessModelID
UNION
SELECT Id, Title FROM [dbo].[RevenueStreams] WHERE BusinessModelId = @BusinessModelID
UNION
SELECT Id, Title FROM [dbo].[Solutions] WHERE BusinessModelId = @BusinessModelID
UNION
SELECT Id, Title FROM [dbo].[Unfairs] WHERE BusinessModelId = @BusinessModelID
UNION
SELECT Id, Title FROM [dbo].[Values] WHERE BusinessModelId = @BusinessModelID

暂无
暂无

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

相关问题 Entity Framework Core 3.1 从存储过程返回值 (int) - Entity Framework Core 3.1 Return value (int) from stored procedure 在 EF Core 3.1 中包含 FromSqlRaw 和存储过程 - Include with FromSqlRaw and stored procedure in EF Core 3.1 实体框架4-如何从存储过程中读取多个记录集? - Entity Framework 4 - How to Read Multiple Record Sets From A Stored Procedure? 在Entity Framework数据库优先方法中,如何从存储过程返回多个结果集? - In Entity Framework database-first approach how to return multiple result sets from a stored procedure? 如何 map 来自 postresql 中的存储过程的结果集,我使用 Entity Framework Core 返回多个游标 - How to map set of results from stored procedure in postresql where I return multiple cursors using Entity Framework Core Entity Framework Core 使用存储过程删除带有 bool 返回值的实体 - Entity Framework Core using stored procedure to delete entity with bool return EF Core 3.1:Sequence 不包含带有 FromSqlRaw 查询存储过程的元素 - EF Core 3.1: Sequence contains no elements with FromSqlRaw query a stored procedure 具有多个结果集的实体框架5存储过程 - Entity Framework 5 stored procedure with multiple result sets Entity Framework Core 3.1 存储过程可选参数不起作用 - Entity Framework Core 3.1 stored procedure optional parameters not working 如何在 Entity Framework Core 中的多个表上使用联接调用存储过程? - How to call Stored Procedure with join on multiple tables in Entity Framework Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM