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);

最新文章

  1. AngularJS Best Practices: resource
  2. The Same Game-POJ1027模拟
  3. Oracle中的数据类型
  4. A trip through the Graphics Pipeline 2011_03
  5. Unity物理投射相关问题整理
  6. Java8新特性 1——利用流和Lambda表达式操作集合
  7. 常用PC服务器LSI阵列卡配置
  8. 关于sscanf函数的各种详细用法
  9. MySQL &#183; 引擎特性 &#183; InnoDB 事务系统
  10. JavaWeb之Java Servlet完全教程(转)
  11. canvas 填充图片
  12. c++ 创建线程以及参数传递
  13. 30. pt-upgrade
  14. CentOS 7.4安装Nginx 1.14.0
  15. 大数据自学2-Hue集成环境中使用Sqoop组件从Sql Server导数据到Hive/HDFS
  16. MySQL,查看连接数和状态等
  17. EasyUI写的登录界面
  18. c++ 判断两个容器是否相等(equal)
  19. win7系统安装VS2013后,连不上远程sqlserver数据库解决办法
  20. Python3 小工具-MAC泛洪

热门文章

  1. C++11 并发指南四(&lt;future&gt; 详解二 std::packaged_task 介绍)
  2. SkylineGlobe系列软件对机器配置要求
  3. 支持“xxxContext”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库
  4. [03] 线程同步 synchronized
  5. FineUI经典项目展示(1)生产在线管理系统
  6. 分布式架构的基石.简单的 RPC 框架实现(JAVA)
  7. C# Type.GetType 返回NULL 问题解决记录
  8. developer的996,需要谁来拯救
  9. VirtualBox安装复制Centos6.6配置网络
  10. 006-筛选分类排序搜索查找Filter-Classificatio-Sort-Search-Find-Seek-Locate