前言:今天來學習下ORM框架中的linq2db,本文將從linq2db的介紹、linq2db的優點、linq2db的簡單使用做個學習記錄
linq2db的介紹
LINQ to DB是最快的LINQ數據庫訪問庫,在POCO對象和數據庫之間提供簡單,輕便,快速且類型安全的層。
linq2db的優點
- 簡單、輕便、快捷、能快速實現CRUD
- 集成了linq語法和lambada表達式的寫法
- 使用T4模板自動生成實體類,不用使用其他代碼生成器了。方便
- 類型安全
linq2db的使用
項目依賴項中使用NuGet程序包添加:linq2db.SqlServer
可以看到有很多linq2db.XXX的,可能不同數據庫需要引入的不同,我們這里用SQLServer,后續用到其他數據庫的時候再補充
引入之后,項目中會多一個文件夾 如下:
我們從文件夾下面找到第一個文件CopyMe.SqlServer.tt.txt 看看文件夾里面的內容如下:
<#@ 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. 在要生成數據模型的文件夾中創建新的*.tt文件(例如MyDatabase.tt)並將此文件中的內容復制到其中 For example: MyProject DataModels MyDatabase.tt 2. Modify the connection settings below to connect to your database. 修改下面的連接設置以連接到數據庫。 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(); #>
步驟如下:
- 在要生成數據模型的文件夾中創建新的*.tt文件(例如MyDatabase.tt)並將此文件中的內容復制到其中
-
修改下面的連接設置以連接到數據庫
-
將連接字符串添加到web/app.config文件
- 是具體使用方法
- 更多用法參考網址:https://linq2db.github.io/articles/T4.html
參照步驟 創建了一個文件夾來存放生成的實體類 如下:
將文件后綴改為*.tt時會提示:
點擊確定,生成的實體類文件如下:
然后點開文件就是連接的數據庫的實體類文件 ,Amazing!!!
然后CopyMe.SqlServer.tt.txt 備注的第三點是用法 ,來看下使用方法
linq2db的RRUD:
- C-Create 創建/新增:
using (var db = new PandaPTPAPDB()) { //1:新增 db.Insert(new Task()); //2:批量新增 db.Insert(new List<Task>()); }
- R-Retrieve 檢索/查詢:
using (var db = new PandaPTPAPDB()) { //1:查詢所有 var list = (from c in db.Tasks select c).ToList(); //2:分頁查詢: int pageIndex = 1, pageSize = 10; var pageList = (from c in db.Tasks select c).Skip((pageIndex - 1) * pageSize + 1).Take(pageSize).ToList(); //3:查詢單條 var model = (from c in db.Tasks select c).FirstOrDefault(x=>x.Id.ToString()=="12345"); }
可以看到訪問數據庫都是通過linq語句來進行的,linq語句的用就很多了,后續完善
- 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的最最最最基本的用法,總的來說,使用起來挺便捷的,不用自己創建實體類,訪問都是基於linq表達式,還是相當方便的。