主題介紹

本文主要介紹Net EntityFramework或 NetEntityFramework Core框架下如何連接瀚高數(shù)據(jù)庫。

驅(qū)動介紹

鏈接:https://pan.baidu.com/s/1xuz6uJz0utRgKWecXhpOiA?pwd=o0tj

瀚高數(shù)據(jù)庫安裝完畢后,自帶數(shù)據(jù)庫驅(qū)動,其位置如下:

image

其中 DotNet 文件夾 即為 Net 程序使用的驅(qū)動包,進入目錄

image

解壓后,可以看到

image

其中列出了 net5.0netcoreapp3.1、netstandard2.0、netstanddard2.1Nhgb.EntityFrameworkcore.HGDB.dll 等幾個文件,根據(jù)開發(fā)環(huán)境和框架,從中選擇適用的驅(qū)動。

Net EntityFramework

Entity Framework (EF)是一個對象關系映射器 (O/RM),它使 .NET 開發(fā)人員能夠使用 .NET 對象處理數(shù)據(jù)庫。它消除了開發(fā)人員通常需要編寫的大多數(shù)數(shù)據(jù)訪問代碼的需求。

EF提供變更跟蹤、唯一性約束、惰性加載、查詢事物等。開發(fā)人員使用Linq語言,對數(shù)據(jù)庫操作如同操作Object對象一樣省事。

EF有三種使用場景,1. 從數(shù)據(jù)庫生成Class,2.由實體類生成數(shù)據(jù)庫表結(jié)構,3. 通過數(shù)據(jù)庫可視化設計器設計數(shù)據(jù)庫,同時生成實體類。

從版本 6 開始,EF 成為一個開源項目,并且完全脫離了 .NET Framework。

目前最新版本為EF 6.4.0。

EF 6 版本歷史

image

Net EntityFrameworkCore

Entity Framework Core 是 EF 6.x 之后的 Entity Framework 的新版本。它是開源、輕量級、可擴展的 Entity Framework 數(shù)據(jù)訪問技術的跨平臺版本。

EF Core 旨在與 .NET Core 應用程序一起使用。但是,它也可以與基于標準 .NET 4.5+ 框架的應用程序一起使用。

目前最新版本為EFCore8.0。

下表列出了 EF 6 和 EF Core 之間的重要區(qū)別。

image

EF Core 版本歷史

image

下圖說明了實體框架在應用程序中的位置

image

綜上所述:使用Net EntityFrameworkCore框架的項目,除了按照目標框架引用 netcoreapp3.1 文件的數(shù)據(jù)庫驅(qū)動外,還需引用Nhgb.EntityFrameworkcore.HGDB.dll 這個Entity框架的依賴。

開發(fā)環(huán)境搭建

軟件 版本
HGDB 安全版V4、企業(yè)版v6及以上版本
visual studio 2019(vs2022中已棄用5.0)

添加引用

image

創(chuàng)建數(shù)據(jù)庫和架構

  • 遷移數(shù)據(jù)庫
Text
Add-Migration InitialCreate
  • 反向工程(基架)
Text
Scaffold-DbContext "Host=192.168.2.5;Port=5866;Database=highgo;Username=sysdba;Password=Hero@123" Nhgdb.EntityFrameworkCore.HGDB -Schemas public -OutputDir Models -UseDatabaseNames -Tables student

示例代碼

Models/Student.cs

Text
[Table("student")]
public class Student
{
[Column("id")]
public int Id { get; set; }

[Column("name")]
public string? Name { get; set; }
[Column("sex")]
public string? Sex { get; set; }
[Column("age")]
public int Age { get; set; }
[Column("grade")]
public int Grade { get; set; }
[Column("address")]
public string? Address { get; set; }
}

Controllers/StudentController.cs

Text
public class StudentController : Controller
{

private readonly HgdbContext _hgDbContext;
public StudentController(HgdbContext hgDbContext)
{
_hgDbContext = hgDbContext;
}

public IActionResult Index()
{
IEnumerable<Student> objCatlist = _hgDbContext.Students.AsNoTracking().ToList();
return View(objCatlist);
}
}

Data/HgdbContext.cs

Text
public class HgdbContext: DbContext
{
public HgdbContext(DbContextOptions<HgdbContext> options)
:base(options){ }
public DbSet<Student> Students { get; set; }
}

View/Student/Index.cshtml

Text
@model IEnumerable<Student>
<div class="container shadow p-5">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th scope="col">序號</th>
<th scope="col">姓名</th>
<th scope="col">性別</th>
<th scope="col">年齡</th>
<th scope="col">年級</th>
<th scope="col">地址</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td width="6%">
@item.Id
</td>
<td width="20%">
@item.Name
</td>
<td width="10%">
@item.Sex
</td>
<td width="10%">
@item.Age
</td>
<td width="10%">
@item.Grade
</td>
<td width="25%">
@item.Address
</td>
</tr>
}
</tbody>
</table>
</div>

appsettings.json

Text
"ConnectionStrings": {
"HgdbConnection": "Server=192.168.2.5;Port=5866;User Id=sysdba;Password=Hero@123;Database=highgo;"
}

startup.cs

Text
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();

services.AddDbContext<HgdbContext>(options =>
{
var connectionString = this.Configuration["ConnectionStrings:HgdbConnection"];
options.UseNhgdb(connectionString);
});
}

結(jié)果演示

image

引用其他版本驅(qū)動的表現(xiàn)

引用 netstanddard2.0

需要引入的依賴包

包名
Microsoft.Bcl.AsyncInterfaces

image

引用 netstanddard2.1

不需引入已依賴包,即可正常運行

image

引用 netcoreapp3.1

不需引入已依賴包,即可正常運行

image

引用 net5.0

拋出錯誤

image

Nuget 提供的 System.Runtime 的版本為 4.3.1

image

引用 net6.0

Nhgdb.dllNhgdb.EntityFrameworkCore.HGDB.dll外,還需要引入目錄下自帶的Microsoft.EntityFrameworkCore.dll,或引入8.0.0-preview.4.23259.3版本的 efcore

生成解決方案報錯

image

從 Nuget 引入Microsoft.Extensions.DependencyInjection.Abstractions 8.0.0 版本后可成功生成解決方案

運行應用,報錯

image

從 Nuget 引入Microsoft.Extensions.Logging.Abstractions 8.0.0 版本后繼續(xù)進行下一步

返回應用頁面,報錯

image

從 Nuget 引入Microsoft.Extensions.Caching.Abstractions 8.0.0 版本后繼續(xù)運行下一步

返回應用頁面,報錯

image

從 Nuget 引入Microsoft.Extensions.DependencyInjection 8.0.0 版本后繼續(xù)運行下一步

image

從 Nuget 引入Microsoft.EntityFrameworkCore.Abstractions 8.0.0 版本時報錯

image

將項目目標框架改為.NET 8.0后,重新引入Microsoft.EntityFrameworkCore.Abstractions

image

完成以上步驟后應用可正常運行

引用 net7.0

問題及操作步驟同《引用 net6.0》不分

引用 net8.0

Nhgdb.dllNhgdb.EntityFrameworkCore.HGDB.dll外,還需要引入目錄下自帶的Microsoft.EntityFrameworkCore.dll,或引入8.0.0-preview.4.23259.3版本的 efcore

重新生成解決方案,運行應用報錯

image

從 Nuget 引入Microsoft.EntityFrameworkCore.Abstractions 8.0.0 版本后,應用可正常運行