How to call Stored Procedures with EntityFramework?

前端 未结 7 1338
青春惊慌失措
青春惊慌失措 2020-12-01 02:03

I have generated an EF4 Model from a MySQL database and I have included both StoredProcedures and Tables.

I know how to make regular instert/update/fetch/delete oper

7条回答
  •  孤街浪徒
    2020-12-01 02:48

    Basically you just have to map the procedure to the entity using Stored Procedure Mapping.

    Once mapped, you use the regular method for adding an item in EF, and it will use your stored procedure instead.

    Please see: This Link for a walkthrough. The result will be adding an entity like so (which will actually use your stored procedure)

    using (var ctx = new SchoolDBEntities())
            {
                Student stud = new Student();
                stud.StudentName = "New sp student";
                stud.StandardId = 262;
    
                ctx.Students.Add(stud);
                ctx.SaveChanges();
            }
    

提交回复
热议问题