dapper也是是一种orm框架

这里记录下,使用asp.net 集成 dapper 的过程,方便自己查看

至于dapper的特性以及操作可以参考dapper官方文档

 

1.创建数据库相关

  • 在sql server 创建一个叫做 dapperdemo 的数据库
  • 再创建一个叫做 products 的表

脚本如下

create table [dbo].[products](
    [productid] [int] identity(1,1) not null,
    [name] [nvarchar](max) null,
    [quantity] [int] null,
    [price] [float] null,
 constraint [pk_products] primary key clustered 
(
    [productid] asc
)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary] textimage_on [primary]
 
go

 

2.创建一个asp.net web api 项目

  • 文件->新建->项目
  • 选择 asp.net core web 应用 的模板,项目名 dapperdemo
  • 在新的 asp.net core web 应用的页面,选择 api 模板,并确定,不要选择支持docker

 

3.增加model实体

  • 右击项目,新增一个models文件夹
  • 在models文件夹下增加一个类(class),product
public class product
{
    [key]
    public int productid { get; set; }
    public string name { get; set; }
    public int quantity { get; set; }
    public double price { get; set; }
}

 

4.引入dapper nuget包

  • 工具->nuget 包管理器 -> 管理解决方案的 nuget 包程序包
  • 搜索dapper ,并且安装

 如下,安装

 

也可以使用  程序包管理器控制台 进行安装

install-package dapper

 

5.使用dapper

  dapper的使用需要下面三步:

  • 使用连接字符串( connection string )创建一个 idbconnection 对象
  • 编写你自己的sql 语句
  • 把 sql 语句传给 dapper

 

所以,操作如下

  • 创建一个repository文件夹
  • 在repository文件夹里增加一个名为 productrepository 的class类

代码如下

 1 public class productrepository
 2 {
 3     private string connectionstring;
 4     public productrepository()
 5     {
 6         connectionstring = @"server=localhost;database=dapperdemo;trusted_connection=true;";
 7     }
 8  
 9     public idbconnection connection
10     {
11         get  {
12             return new sqlconnection(connectionstring);
13         }
14     }
15  
16     public void add(product prod)
17     {
18         using (idbconnection dbconnection = connection)
19         {
20             string squery = "insert into products (name, quantity, price)"
21                             + " values(@name, @quantity, @price)";
22             dbconnection.open();
23             dbconnection.execute(squery, prod);
24         }
25     }
26  
27     public ienumerable<product> getall()
28     {
29         using (idbconnection dbconnection = connection)
30         {
31             dbconnection.open();
32             return dbconnection.query<product>("select * from products");
33         }
34     }
35  
36     public product getbyid(int id)
37     {
38         using (idbconnection dbconnection = connection)
39         {
40             string squery = "select * from products"
41                            + " where productid = @id";
42             dbconnection.open();
43             return dbconnection.query<product>(squery, new { id = id }).firstordefault();
44         }
45     }
46  
47     public void delete(int id)
48     {
49         using (idbconnection dbconnection = connection)
50         {
51              string squery = "delete from products"
52                           + " where productid = @id";
53             dbconnection.open();
54             dbconnection.execute(squery, new { id = id });
55         }
56     }
57  
58     public void update(product prod)
59     {
60         using (idbconnection dbconnection = connection)
61         {
62             string squery = "update products set name = @name,"
63                            + " quantity = @quantity, price= @price"
64                            + " where productid = @productid";
65             dbconnection.open();
66             dbconnection.query(squery, prod);
67         }
68     }
69 }

 

这里的连接字符串是直接写在代码里的,可以根据需要自己调整

 

6.创建controller

  • 创建一个名为 productcontroller 的类

代码如下

 1 [route("api/[controller]")]
 2 public class productcontroller : controller
 3 {
 4     private readonly productrepository productrepository;
 5     public productcontroller()
 6     {
 7         productrepository = new productrepository();
 8     }
 9     // get: api/values
10     [httpget]
11     public ienumerable<product> get()
12     {
13         return productrepository.getall();
14     }
15  
16     // get api/values/5
17     [httpget("{id}")]
18     public product get(int id)
19     {
20         return productrepository.getbyid(id);
21     }
22  
23     // post api/values
24     [httppost]
25     public void post([frombody]product prod)
26     {
27         if (modelstate.isvalid)
28             productrepository.add(prod);
29     }
30  
31     // put api/values/5
32     [httpput("{id}")]
33     public void put(int id, [frombody]product prod)
34     {
35         prod.productid = id;
36         if (modelstate.isvalid)
37             productrepository.update(prod);
38     }
39  
40     // delete api/values/5
41     [httpdelete("{id}")]
42     public void delete(int id)
43     {
44         productrepository.delete(id);
45     }
46 }

 

7.运行,验证是否成功

在这之前,可以手动往数据库表里加几条数据,我这里没有加,只是在get方法里打了个断点

在浏览器中输入 https://localhost:44315/api/product

 

 因为我数据库里没有数据,这里返回的空的

 

这里做记录方便查看,如有错误,欢迎指正

参考网址:

Use Dapper ORM With ASP.NET Core