zoukankan      html  css  js  c++  java
  • Entity Framework Tutorial Basics(29):Stored Procedure in Entity Framework

    Stored Procedure in Entity Framework:

    Entity Framework has the ability to automatically build native commands for the database based on your LINQ to Entities or Entity SQL queries, as well as, build the commands for inserting, updating, or deleting data. You may want to override these steps and use your own predefined stored procedures. You can use stored procedures either to get the data or to add/update/delete the records to one or multiple database tables.

    Stored procedures and user-defined functions (UDFs) in the database are represented as functions in entity framework. EDM won't have any entity for the stored procedures in the EDM designer.

    Here, we will add the following stored procedure GetCoursesByStudentId into EDM. This procedure returns all the courses assigned to a particular student:

    CREATE PROCEDURE [dbo].[GetCoursesByStudentId]
        -- Add the parameters for the stored procedure here
        @StudentId int = null
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        -- Insert statements for procedure here
    select c.courseid, c.coursename,c.Location, c.TeacherId
    from student s 
    left outer join studentcourse sc on sc.studentid = s.studentid 
    left outer join course c on c.courseid = sc.courseid
    where s.studentid = @StudentId
    END

    First, create a new ADO.Net Entity Data Model using EF Designer from database.

    Entity Framework stored procedure

    Select GetCoursesByStudentId. Make sure that the Import selected stored procedures and functions into the entity model checkbox is selected and then click Finish.

    Entity Framework stored procedure

    You will see GetCoursesByStudentId added in Function Imports with new complex type GetCoursesByStudentId_Result in the Model Browser. Whenever you import a stored procedure into a model, it creates a new complex type with the name {sp name}_Result by default.

    Entity Framework stored procedure

    GetCoursesByStudentId returns the same fields defined in Course entity. So we don't need to add a new complex type for the returned data from GetCoursesByStudentId. You can change it by right clicking on GetCoursesByStudentId in function imports and selecting Edit. Check Entities and select Course from dropdown in popup window as shown below:

    Entity Framework stored procedureEntity Framework stored procedure

    You will see the function in the context class for GetCoursesByStudentId as shown below:

    Entity Framework stored procedure

    Now, GetCoursesByStudentId can be called and the result shown below will be returned:

    using (var context = new SchoolDBEntities())
    {
        var courses = context.GetCoursesByStudentId(1);
    
        foreach (Course cs in courses)
            Console.WriteLine(cs.CourseName);
    }

    The code shown above will execute the following statement:

    exec [dbo].[GetCoursesByStudentId] @StudentId=1

    You will learn how to use stored procedures for CUD operation in the next chapter. 

  • 相关阅读:
    php http_build_query 将布尔值类型转为整型的问题
    一天一个 Linux 命令(34):free 命令
    Laravel 如何使用 PHP 内置的服务器启动服务
    一天一个 Linux 命令(33):top 命令
    Java基础(5)-Java数据类型
    Java int和Integer有什么区别
    Java异常处理常见问题
    PHP重载,不一样的重载
    nginx 反向代理 proxy_pass详解
    composer repositories仓库配置,命令行修改仓库地址
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5649176.html
Copyright © 2011-2022 走看看