locked
Using Stored procedures as methods in CodeFirst Approach RRS feed

  • Question

  • User1037983650 posted

    Hello Folks, 

    I am working with CodeFirst apporoach in my application. So there is not any .edmx or .dbml file in the solution. Tables are being created by using PMC commands. I have some stored procedure in Database and I need them to be in application so that I can use them same as a Method (As we do while working with .edmx and .dbml file - by dragging and dropping ).

    How would this be possible in CodeFirst Approach?

    Please assist. 

    Thanks !!

    Tuesday, August 15, 2017 6:37 AM

Answers

  • User-335504541 posted

    Hi Sukhdeep,

    Do you want to call your sp in your code?

    You could call your SP by using :

                    using (var db = new DbContext())
                    {
                      db.Database.SqlQuery<YourEntityType>("storedProcedureName",params);
                    }

    And you could refer to links below for more information:

    https://www.mikesdotnetting.com/article/299/entity-framework-code-first-and-stored-procedures

    https://stackoverflow.com/questions/41341854/call-stored-procedures-with-entity-framework-6-code-first-approach-in-asp-net-mv

    Best Regards,

    Billy

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 15, 2017 8:21 AM
  • User-335504541 posted

    Hi Sukhdeep,

    I think you could try to create a method in your DbContext to call the SP.

    For example:

            public List<YourEntityType> storedProcedureName(string parame1)
            {
    return this.Database.SqlQuery<YourEntityType>("storedProcedureName", parame1).ToList(); }

    Then you could call it by using:

    var Entities = db.storedProcedureName("parame1");
    

    You could also create the method in other class.

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 16, 2017 2:17 AM

All replies

  • User-335504541 posted

    Hi Sukhdeep,

    Do you want to call your sp in your code?

    You could call your SP by using :

                    using (var db = new DbContext())
                    {
                      db.Database.SqlQuery<YourEntityType>("storedProcedureName",params);
                    }

    And you could refer to links below for more information:

    https://www.mikesdotnetting.com/article/299/entity-framework-code-first-and-stored-procedures

    https://stackoverflow.com/questions/41341854/call-stored-procedures-with-entity-framework-6-code-first-approach-in-asp-net-mv

    Best Regards,

    Billy

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 15, 2017 8:21 AM
  • User1037983650 posted

    Hi Billy Thanks for replying. So I could conclude that we cannot use Stored Procedure as a methods where we get the intellisense about the parms to be passed? Am I right?

    we have to use 

    db.Database.SqlQuery then map the result with custom type.
    Right?

    Tuesday, August 15, 2017 2:41 PM
  • User-335504541 posted

    Hi Sukhdeep,

    I think you could try to create a method in your DbContext to call the SP.

    For example:

            public List<YourEntityType> storedProcedureName(string parame1)
            {
    return this.Database.SqlQuery<YourEntityType>("storedProcedureName", parame1).ToList(); }

    Then you could call it by using:

    var Entities = db.storedProcedureName("parame1");
    

    You could also create the method in other class.

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 16, 2017 2:17 AM
  • User1037983650 posted

    Hi Billy, that sounds good to me. Thanks!!

    Wednesday, August 16, 2017 4:20 AM
  • User1037983650 posted

    a quick query - Wouldn't it make the DataContext heav if do same thing for 100 stored procedures ? I have many Stored procedures in my app. what would be the impact if I do same with all?

    Wednesday, August 16, 2017 4:25 AM
  • User-335504541 posted

    Hi Sukhdeep,

    I think it will not make a lots difference.
    The performance also depends on other property.
    You could refer to link below for an article the performance of EF:

    https://msdn.microsoft.com/en-us/library/hh949853(v=vs.113).aspx

    Best Regards,

    Billy

    Wednesday, August 16, 2017 5:09 AM