主题介绍

本文主要介绍Net EntityFramework或 NetEntityFramework Core框架下如何连接瀚高数据库。

驱动介绍

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

瀚高数据库安装完毕后,自带数据库驱动,其位置如下:

image

其中 DotNet 文件夹 即为 Net 程序使用的驱动包,进入目录

image

解压后,可以看到

image

其中列出了 net5.0netcoreapp3.1netstandard2.0netstanddard2.1Nhgb.EntityFrameworkcore.HGDB.dll 等几个文件,根据开发环境和框架,从中选择适用的驱动。

Net EntityFramework

Entity Framework (EF)是一个对象关系映射器 (O/RM),它使 .NET 开发人员能够使用 .NET 对象处理数据库。它消除了开发人员通常需要编写的大多数数据访问代码的需求。

EF提供变更跟踪、唯一性约束、惰性加载、查询事物等。开发人员使用Linq语言,对数据库操作如同操作Object对象一样省事。

EF有三种使用场景,1. 从数据库生成Class,2.由实体类生成数据库表结构,3. 通过数据库可视化设计器设计数据库,同时生成实体类。

从版本 6 开始,EF 成为一个开源项目,并且完全脱离了 .NET Framework。

目前最新版本为EF 6.4.0。

EF 6 版本历史

image

Net EntityFrameworkCore

Entity Framework Core 是 EF 6.x 之后的 Entity Framework 的新版本。它是开源、轻量级、可扩展的 Entity Framework 数据访问技术的跨平台版本。

EF Core 旨在与 .NET Core 应用程序一起使用。但是,它也可以与基于标准 .NET 4.5+ 框架的应用程序一起使用。

目前最新版本为EFCore8.0。

下表列出了 EF 6 和 EF Core 之间的重要区别。

image

EF Core 版本历史

image

下图说明了实体框架在应用程序中的位置

image

综上所述:使用Net EntityFrameworkCore框架的项目,除了按照目标框架引用 netcoreapp3.1 文件的数据库驱动外,还需引用Nhgb.EntityFrameworkcore.HGDB.dll 这个Entity框架的依赖

开发环境搭建

软件 版本
HGDB 安全版V4、企业版v6及以上版本
visual studio 2019(vs2022中已弃用5.0)

添加引用

image

创建数据库和架构

  • 迁移数据库
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);
});
}

结果演示

image

引用其他版本驱动的表现

引用 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 版本后继续进行下一步

返回应用页面,报错

image

从 Nuget 引入Microsoft.Extensions.Caching.Abstractions 8.0.0 版本后继续运行下一步

返回应用页面,报错

image

从 Nuget 引入Microsoft.Extensions.DependencyInjection 8.0.0 版本后继续运行下一步

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 版本后,应用可正常运行