Dapper也是是一種ORM框架sql
這裏記錄下,使用ASP.NET 集成 Dapper 的過程,方便本身查看數據庫
至於Dapper的特性以及操做能夠參考Dapper官方文檔api
腳本以下瀏覽器
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
public class Product { [Key] public int ProductId { get; set; } public string Name { get; set; } public int Quantity { get; set; } public double Price { get; set; } }
以下,安裝app
也能夠使用 程序包管理器控制檯 進行安裝框架
Install-Package Dapper
Dapper的使用須要下面三步:工具
因此,操做以下spa
代碼以下.net
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 }
這裏的鏈接字符串是直接寫在代碼裏的,能夠根據須要本身調整code
代碼以下
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 }
在這以前,能夠手動往數據庫表裏加幾條數據,我這裏沒有加,只是在Get方法裏打了個斷點
在瀏覽器中輸入 https://localhost:44315/api/product
由於我數據庫裏沒有數據,這裏返回的空的
這裏作記錄方便查看,若有錯誤,歡迎指正
參考網址:
https://www.talkingdotnet.com/use-dapper-orm-with-asp-net-core/