ASP.NET MVC: Best Way To Call Stored Procedure

前端 未结 5 1218
梦毁少年i
梦毁少年i 2020-12-08 05:48

I\'m trying to decide which is the best way to call a stored procedure.

I\'m new to ASP.NET MVC and I\'ve been reading a lot about Linq to SQL and Entity Framework,

5条回答
  •  盖世英雄少女心
    2020-12-08 06:06

    Try this:

    Read:

    var authors = context.Database.SqlQuery("usp_GetAuthorByName @AuthorName", 
    new SqlParameter("@AuthorName", "author"));
    

    Update:

    var affectedRows = context.Database.ExecuteSqlCommand
    ("usp_CreateAuthor @AuthorName = {0}, @Email= {1}", 
    "author", "email");
    

    From this link: http://www.dotnetthoughts.net/how-to-execute-a-stored-procedure-with-entity-framework-code-first/

    And I would go with the framework David Lively mentioned, instead of having the routines in the controller. Simply pass the results back as IEnumerable from a function in a separate repository class for an edit, pass a boolean back for if the update succeeded for an update.

提交回复
热议问题