Is a DbSet required to run a stored procedure?

南楼画角 提交于 2021-01-29 11:06:05

问题


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();
}

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.

Thanks,


回答1:


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

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).

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).

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



来源:https://stackoverflow.com/questions/58996385/is-a-dbset-required-to-run-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!