ORM之linq2db
前言:今天來(lái)學(xué)習(xí)下ORM框架中的linq2db,本文將從linq2db的介紹、linq2db的優(yōu)點(diǎn)、linq2db的簡(jiǎn)單使用做個(gè)學(xué)習(xí)記錄
linq2db的介紹
LINQ to DB是最快的LINQ數(shù)據(jù)庫(kù)訪問(wèn)庫(kù),在POCO對(duì)象和數(shù)據(jù)庫(kù)之間提供簡(jiǎn)單,輕便,快速且類(lèi)型安全的層。
linq2db的優(yōu)點(diǎn)
- 簡(jiǎn)單、輕便、快捷、能快速實(shí)現(xiàn)CRUD
- 集成了linq語(yǔ)法和lambada表達(dá)式的寫(xiě)法
- 使用T4模板自動(dòng)生成實(shí)體類(lèi),不用使用其他代碼生成器了。方便
- 類(lèi)型安全
linq2db的使用
項(xiàng)目依賴(lài)項(xiàng)中使用NuGet程序包添加:linq2db.SqlServer

可以看到有很多l(xiāng)inq2db.XXX的,可能不同數(shù)據(jù)庫(kù)需要引入的不同,我們這里用SQLServer,后續(xù)用到其他數(shù)據(jù)庫(kù)的時(shí)候再補(bǔ)充
引入之后,項(xiàng)目中會(huì)多一個(gè)文件夾 如下:
我們從文件夾下面找到第一個(gè)文件CopyMe.SqlServer.tt.txt 看看文件夾里面的內(nèi)容如下:
<#@ template language="C#" debug="True" hostSpecific="True" #> <#@ output extension=".generated.cs" #> <#@ include file="$(LinqToDBT4SqlServerTemplatesPath)LinqToDB.SqlServer.Tools.ttinclude" once="true" #> <#@ include file="$(LinqToDBT4SqlServerTemplatesPath)PluralizationService.ttinclude" once="true" #> <# /* 1. Create new *.tt file (e.g. MyDatabase.tt) in a folder where you would like to generate your data model and copy content from this file to it. 在要生成數(shù)據(jù)模型的文件夾中創(chuàng)建新的*.tt文件(例如MyDatabase.tt)并將此文件中的內(nèi)容復(fù)制到其中 For example: MyProject DataModels MyDatabase.tt 2. Modify the connection settings below to connect to your database. 修改下面的連接設(shè)置以連接到數(shù)據(jù)庫(kù)。 3. Add connection string to the web/app.config file: 將連接字符串添加到web/app.config文件: <connectionStrings> <add name="MyDatabase" providerName="System.Data.SqlClient" connectionString="Data Source=.;Database=MyDatabase;User Id=User;Password=TestPassword;" /> </connectionStrings> 4. To access your database use the following code: using (var db = new MyDatabaseDB()) { var q = from c in db.Customers select c; foreach (var c in q) Console.WriteLine(c.ContactName); } 5. See more at https://linq2db.github.io/articles/T4.html If you need to use the Microsoft.SqlServer.Types namespace, install the Microsoft.SqlServer.Types nuget, and replace the following include at the top of this file: "$(ProjectDir)LinqToDB.Templates\LinqToDB.SqlServer.Tools.ttinclude" with "$(ProjectDir)LinqToDB.Templates\LinqToDB.SqlServer.SqlTypes.Tools.ttinclude" IMPORTANT: if running .tt file gives you error like this: "error : Failed to resolve include text for file: C:\...\$(LinqToDBT4<DB>TemplatesPath)LinqToDB.<DB>.Tools.ttinclude" check tt file properties. Custom tool must be set to TextTemplatingFileGenerator, not TextTemplatingFilePreprocessor or any other value. */ NamespaceName = "DataModels"; // to configure GetSchemaOptions properties, add them here, before load metadata call LoadSqlServerMetadata("MyServer", "MyDatabase", "User", "Password"); // LoadSqlServerMetadata(".", "MyDatabase"); // Integrated Security // LoadSqlServerMetadata(string connectionString); // to adjust loaded database model before generation, add your code here, after load metadata, but before GenerateModel() call GenerateModel(); #>
步驟如下:
- 在要生成數(shù)據(jù)模型的文件夾中創(chuàng)建新的*.tt文件(例如MyDatabase.tt)并將此文件中的內(nèi)容復(fù)制到其中
-
修改下面的連接設(shè)置以連接到數(shù)據(jù)庫(kù)
-
將連接字符串添加到web/app.config文件
- 是具體使用方法
- 更多用法參考網(wǎng)址:https://linq2db.github.io/articles/T4.html
參照步驟 創(chuàng)建了一個(gè)文件夾來(lái)存放生成的實(shí)體類(lèi) 如下:

將文件后綴改為*.tt時(shí)會(huì)提示:

點(diǎn)擊確定,生成的實(shí)體類(lèi)文件如下:

然后點(diǎn)開(kāi)文件就是連接的數(shù)據(jù)庫(kù)的實(shí)體類(lèi)文件 ,Amazing!!!
然后CopyMe.SqlServer.tt.txt 備注的第三點(diǎn)是用法 ,來(lái)看下使用方法
linq2db的RRUD:
- C-Create 創(chuàng)建/新增:
using (var db = new PandaPTPAPDB()) { //1:新增 db.Insert(new Task()); //2:批量新增 db.Insert(new List<Task>()); }
- R-Retrieve 檢索/查詢(xún):
using (var db = new PandaPTPAPDB()) { //1:查詢(xún)所有 var list = (from c in db.Tasks select c).ToList(); //2:分頁(yè)查詢(xún): int pageIndex = 1, pageSize = 10; var pageList = (from c in db.Tasks select c).Skip((pageIndex - 1) * pageSize + 1).Take(pageSize).ToList(); //3:查詢(xún)單條 var model = (from c in db.Tasks select c).FirstOrDefault(x=>x.Id.ToString()=="12345"); }
可以看到訪問(wèn)數(shù)據(jù)庫(kù)都是通過(guò)linq語(yǔ)句來(lái)進(jìn)行的,linq語(yǔ)句的用就很多了,后續(xù)完善
- U-Update 更新:
using (var db = new PandaPTPAPDB()) { //1:更新 db.Update(new Task()); //2:批量更新 db.Update(new List<Task>()); }
- D-Delete 刪除:
using (var db = new PandaPTPAPDB()) { //1:刪除 db.Delete(new Task()); //2:批量刪除 db.Delete(new List<Task>()); }
以上就是linq2db的最最最最基本的用法,總的來(lái)說(shuō),使用起來(lái)挺便捷的,不用自己創(chuàng)建實(shí)體類(lèi),訪問(wèn)都是基于linq表達(dá)式,還是相當(dāng)方便的。

浙公網(wǎng)安備 33010602011771號(hào)