2

I am working on a project where code first E-F was implemented but not able to access the stored procedures.How could I add them to the model now?

  • Why do you need stored procedures when using code first entity framework? You would normally use the controllers to incorporate the logic that a stored procedure would. – Rick Nov 02 '17 at 09:24
  • I am working on an existing project.I want to use the stored procedures.It will be much time consuming in re building existing logic.Cant we access the store procedures now? – Praveen Kumar Rejeti Nov 02 '17 at 09:32

1 Answers1

1

Well, if it's an existing database, you could just use Microsoft SQL Server Management Studio to access the stored procedures. They can be found under 'Your database' > 'Programmability' > 'Stored Procedures' in the Object Explorer. Though I'm pretty sure that using both stored procedures AND entity framework is not common practice.

To answer your question, right-click the Stored Procedures filter in the Object Explorer mentioned above and click the Stored Procedure option to add a stored procedure to your database.

If you don't want to use MSSMS, you can also add stored procedures in C# using the migration functionality that entity framework provides, explained in this question.

Rick
  • 1,172
  • 11
  • 25
  • Actually Stored procedures were already there in my db.When implementing the Code first ef, might forgot to add Stored Procedures in "OnModelCreating" method along with the db tables.Now I want to add the SPs to the existing Entity model.Is that was possible? – Praveen Kumar Rejeti Nov 02 '17 at 10:05
  • 1
    You might be able to bind a model to your stored procedures using `modelBuilder.Entity().MapToStoredProcedures();`, which is explained on [this MSDN page.](https://msdn.microsoft.com/en-us/library/dn468673(v=vs.113).aspx) – Rick Nov 02 '17 at 10:20
  • Yes I have Added this to the OnModelCreating mrthod, and Built the solution.But no luck. Do I need to run the migrations?Once again? – Praveen Kumar Rejeti Nov 02 '17 at 10:29
  • 1
    The OnModelCreating method is called when you first use your data model, or when you use an initialize method in your startup code: `Database.SetInitializer(new CreateDatabaseIfNotExists());` – Rick Nov 02 '17 at 10:39