• How to execute a Stored Procedure with Entity Framework Code First


    Recently I worked on a project, which I started as code first and then I forced to switch to Database first. This post is about executing procedures from EF code first.(This is an update version of this post Here is my class structure and procedures.

    class DatabaseContext : DbContext
    {
        public DbSet<Book> Books { get; set; }
        public DbSet<Author> Authors { get; set; }
    }
    
    class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ISBN { get; set; }
        public int AuthorId { get; set; }
    }
    
    class Author
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }

    And here is my stored procedures

    CREATE PROCEDURE usp_CreateBook
    @BookName VARCHAR(200), @ISBN VARCHAR(200), @BookId INT OUTPUT
    AS
    SET NOCOUNT ON
    INSERT INTO Books(Name, ISBN, AuthorId) VALUES(@BookName, @ISBN, 1)
    SET @BookId = (SELECT SCOPE_IDENTITY())
    
    CREATE PROCEDURE usp_CreateAuthor
    @AuthorName VARCHAR(200), @Email VARCHAR(200) = NULL
    AS
    INSERT INTO Authors(Name, Email) VALUES(@AuthorName, @Email)
    
    CREATE PROCEDURE usp_GetAuthorByName
    @AuthorName VARCHAR(200)
    AS
    SELECT [Id] ,[Name] ,[Email] FROM [Authors]
    WHERE Name = @AuthorName

    And you can execute using DbContext.Database class. The DbContext.Database.ExecuteSqlCommand() method helps to executes the given DDL/DML command against the database. And it will return the number of rows affected.

    var affectedRows = context.Database.ExecuteSqlCommand("usp_CreateAuthor @AuthorName, @Email",
        new SqlParameter("@AuthorName", "author"),
        new SqlParameter("@Email", "email"));

    Or you can use without creating the SqlParameters.

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

    The DbContext.Database.SqlQuery method helps to return elements of the given generic type. The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type.

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

    This method will return an DbRawSqlQuery, which you can enumerate using For / ForEach loop. For executing procedure with output parameter.

    var bookIdParameter = new SqlParameter();
    bookIdParameter.ParameterName = "@BookId";
    bookIdParameter.Direction = ParameterDirection.Output;
    bookIdParameter.SqlDbType = SqlDbType.Int;
    var authors = context.Database.ExecuteSqlCommand("usp_CreateBook @BookName, @ISBN, @BookId OUT",
        new SqlParameter("@BookName", "Book"),
        new SqlParameter("@ISBN", "ISBN"),
        bookIdParameter);
    Console.WriteLine(bookIdParameter.Value);
  • 相关阅读:
    hikariCP性能调优
    Mysql 8.0 my.ini 系统变量设置Server System Variable Reference
    MySQL性能测试 : 新的InnoDB Double Write Buffermysql .dblwr
    MySQL MyISAM/InnoDB高并发优化经验
    解决NAVICAT 无法连接MYSQL8.0.12_可视化工具无法连接 MYSQL 8.0
    quartz 节点争抢Job 问题算法
    认识Flink中的Window
    多个用户同时update同一张表中的同一条记录会导致死锁吗?MySQL数据库?
    java.util.Base64 基本使用
    解决JDK1.8 编译时提示 程序包com.sun.image.codec.jpeg不存在的问题
  • 原文地址:https://www.cnblogs.com/Javi/p/6541843.html
Copyright © 2020-2023  润新知