问题
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