I am generating Entity Framework classes in a .NET Core application using this command:
Scaffold-DbContext "Server=server name;Database=DB name;user id=user name;password=password;Trusted_Connection=False;Encrypt=True;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models/DB
It is working well and creating models for all the tables and views in the database. But strangely it's not creating models for stored procedures.
Is there anything I am missing in scaffold syntax?
Entity Framework Core creates entity classes only for tables; stored procedures or views are still not supported.
But there is a Github issue tracking on it that you can find more info: Stored procedure mapping support
How to add stored procedures (like a normal entity) after scaffolding:
Create the output model of your stored procedure:
public class SampleModel
{
public int ID { get; set; }
public string Name { get; set; }
}
Add a new DbSet
to your context :
public virtual DbSet<SampleModel> SampleModels { get; set; }
Use your model for the output:
var sampleModels = _context.SampleModels.FromSql($"EXEC ReturnAllSampleModels").ToList();
Done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With