Dapper也是是一種ORM框架 這裡記錄下,使用ASP.NET 集成 Dapper 的過程,方便自己查看 至於Dapper的特性以及操作可以參考Dapper官方文檔 1.創建資料庫相關 在Sql Server 創建一個叫做 DapperDemo 的資料庫 再創建一個叫做 Products 的表 腳 ...
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
因為我資料庫里沒有數據,這裡返回的空的
這裡做記錄方便查看,如有錯誤,歡迎指正
參考網址:
https://www.talkingdotnet.com/use-dapper-orm-with-asp-net-core/