运行存储过程是否需要DbSet

运行存储过程是否需要DbSet

本文介绍了运行存储过程是否需要DbSet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,该存储过程从多表查询返回数据.为此,我需要为查询中涉及的每个表创建一个DbSet吗?我发现所有使用FromSql的示例在FromSql子句之前指定了一个DbSet(例如,下面的示例中的Books).

I have a stored procedure that returns data from a multi-table query. To do this do I need to create a DbSet for each of the tables that are involved in the query? All the examples I find that use FromSql have a DbSet (e.g., Books in the below example) specified before the FromSql clause.

using (var context = new SampleContext())
{
    var books = context.Books
        .FromSql("EXEC GetAllBooks")
        .ToList();
}

我的理解是DbSet代表一个表.请注意,我正在使用现有的数据库,因此未使用EF生成表.

My understanding is a DbSet represents an table. Note that I am working against an existing DB so am not using EF to generate the tables.

谢谢

推荐答案

我假设EFCore给出了FromSql的标记和用法.

I'm assuming EFCore given the tag and usage of FromSql.

对于EFCore 2.1+,我将使用DbQuery( https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.entityframeworkcore.dbquery-1?view=efcore-2.2 ).

For EFCore 2.1+ I would use a DbQuery (https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbquery-1?view=efcore-2.2).

对于已弃用DbQuery的EFCore 3.0,替换为无密钥DbSet( https://docs.microsoft.com/zh-cn/ef/core/modeling/keyless-entity-types ).

For EFCore 3.0, which has deprecated DbQuery, the replacement is a keyless DbSet (https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types).

我已经用存储的proc完成了前者,它可以按您期望的那样工作.

I've done the former with stored procs and it works as you'd expect.

这篇关于运行存储过程是否需要DbSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:09