3

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

2

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:

  1. Create the output model of your stored procedure:

    public class SampleModel
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    
  2. Add a new DbSet to your context :

    public virtual DbSet<SampleModel> SampleModels { get; set; }
    
  3. Use your model for the output:

    var sampleModels = _context.SampleModels.FromSql($"EXEC ReturnAllSampleModels").ToList();
    

Done.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
XAMT
  • 1,515
  • 2
  • 11
  • 31
  • thanks for your response. When I scaffold, it generated tables and Views models. Only storedprocs are not included. It seems like they have extended support for views but sps are still missing. – urvish mandaliya Apr 01 '20 at 16:31
  • @urvishmandaliya; you can add sp's to your model as a said. please mark answered as an accepted if it helps. – XAMT Apr 01 '20 at 16:38
  • 1
    thanks for your helped. It worked. I am able to call the SP now. But the issue I am facing with parameters that I am passing params but everytime generating 0 results set. – urvish mandaliya Apr 01 '20 at 17:05