I have a stored procedure in Mysql that returns a table.
Using of old methods like _context.database.SqlQuery
doesn't work anymore.
The _context.database.Execute*
only contains methods that returns number of affected rows.
In my scenario I'm using scaffolding and can't create database objects from my code, but I can create classes.
The following code (and/or similar tries with Set
or Query
that is obsolete)
_context.Set<MyModel>().FromSql("CALL My_USP({0});", parametervalue).ToList<MyModel>();
returns an error about that the model is not in the _context
because I'm scaffolding and MyModel
is a class from my Models.
I'm totally lost with this and all help I can find in S.O. or Google are about EF6, that doesn't work in my case, the libraries are different.
Any workaround will be appreciated also, if this is not possible to do.
Add to protected override void OnModelCreating(ModelBuilder modelBuilder)
in the context file:
modelBuilder.Entity<MyModel>().HasNoKey();
and then call:
_context.Set<MyModel>().FromSqlRaw("CALL My_USP({0});", parametervalue).ToList<MyModel>();