本文介绍如何更换框架默认的数据库,下面以MySQL数据库为例:

操作步骤

  1. 双击KIMS.Shared项目,打开项目文件,引用MySqlConnector数据库访问包
<PackageReference Include="MySqlConnector" Version="2.3.3" />

其他数据库访问包如下:

//SQLite
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.1" />
//PG系列
<PackageReference Include="Npgsql" Version="8.0.1" />
//Access
<PackageReference Include="System.Data.OleDb" Version="8.0.0" />
//SqlServer
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
  1. 双击打开AppConfig.cs文件,修改数据库配置
info.Connections = [new ConnectionInfo
{
    Name = "Default",
    DatabaseType = DatabaseType.MySql,
    ProviderType = typeof(MySqlConnector.MySqlConnectorFactory)
}];

其他数据库ProviderType如下:

//SQLite
ProviderType = typeof(Microsoft.Data.Sqlite.SqliteFactory)
//PG系列
ProviderType = typeof(Npgsql.NpgsqlFactory)
//Access
ProviderType = typeof(System.Data.OleDb.OleDbFactory)
//SqlServer
ProviderType = typeof(System.Data.SqlClient.SqlClientFactory)
  1. 双击打开appsettings.json文件,修改数据库连接
"ConnString": "Data Source=localhost;port=3306;Initial Catalog=xxx;user id=xxx;password=xxx;Charset=utf8;SslMode=none;AllowZeroDateTime=True;"

其他数据库数据库连接如下:

//SQLite
Data Source=..\\KIMS.db
//Access
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=KIMS;Jet OLEDB:Database Password={password}
//SqlServer
Data Source=localhost;Initial Catalog=KIMS;User Id={userId};Password={password};
//SqlServerLocalDB
Server=(localdb)\\MSSQLLocalDB;Database=KIMS;Trusted_Connection=True
  1. 打开数据库管理工具,创建空白数据库
create database xxx
  1. 运行测试

视频教程

https://www.bilibili.com/video/BV1FU421f7YK/


清风大侠
2 声望1 粉丝

Known开源框架作者。