在GitHub上有很高的人氣 1377星,幾年來做者一直在更新html
當前版本6.0.317 - Netstandard 2.0(同時支持.net core、.net framework4.0+),也可下載單獨的平臺版本git
github: https://github.com/CollaboratingPlatypus/PetaPocogithub
1.添加Nugit引用,搜索(PetaPoco)數據庫
此處下載的是第一個,會建立如下內容,所說的單文件就是隻有PetaPoco.cs一個文件,項目中也可只保留此文件;Generated文件夾存放的是T4模板,用於生成實體類,如不須要能夠刪除app
2.配置數據庫鏈接,根據T4模板生成Model類,如不須要則可省略此步ide
<add name="SqlServer" connectionString="Data Source=.;uid=sa;pwd=we;database=JD;" providerName="System.Data.SqlClient"/>
3.保存後,會生成Database.cs文件內容以下:性能
4.若是數據庫表有新增或修改字段,執行如下操做後,會自動生成相應的實體類。單元測試
3.實操:測試
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PetaPocoDemo { class Program { static void Main(string[] args) { Person p1 = Db.Record<Person>.SingleOrDefault(1); //查詢ID=1的記錄 Db.Record<Person>.Delete(2); //刪除ID=2的記錄 Person person = new Person { Name = "曹操", Sex = "男", Age = 500 }; var db = Db.GetInstance(); db.Execute("truncate table person"); //保存一個實體 db.Save(person); db.Save(new Person { Name = "孫悟空" }); db.Save(new Person { Name = "孫悟空" }); db.Save("Person", "ID", new { Name = "張每天", ID = 3 }); //save根據主鍵決定是新增仍是修改 //查詢一個實體 person = db.Single<Person>(1); person = db.Single<Person>("where id=2"); //刪除一個實體 db.Delete(person); db.Delete<Person>(1); db.Delete<Person>("where name='張每天'"); } } }
CREATE TABLE [dbo].[Person]( [ID] [INT] IDENTITY(1,1) NOT NULL, [Name] [VARCHAR](50) NULL, [Sex] [VARCHAR](50) NULL, [Age] [INT] NULL, [AddTime] [DATETIME] NULL, CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO