更换默认数据库,分三步走。
- 打开工程文件,更换数据库提供者,模板默认提供如下提供者,只需选择其一
<PackageReference Include="System.Data.OleDb" Version="9.0.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
<PackageReference Include="MySqlConnector" Version="2.4.0" />
<PackageReference Include="Npgsql" Version="8.0.5" />
<PackageReference Include="DM.DmProvider" Version="8.3.1.28188" />
- 然后在
AddKnownData
方法中,更换框架默认数据库
services.AddKnownData(option =>
{
var connString = configuration.GetSection("ConnString").Get<string>();
//option.AddAccess<System.Data.OleDb.OleDbFactory>(connString);
option.AddSQLite<Microsoft.Data.Sqlite.SqliteFactory>(connString);
//option.AddSqlServer<Microsoft.Data.SqlClient.SqlClientFactory>(connString);
//option.AddMySql<MySqlConnector.MySqlConnectorFactory>(connString);
//option.AddPgSql<Npgsql.NpgsqlFactory>(connString);
//option.AddDM<Dm.DmClientFactory>(connString);
//option.SqlMonitor = c => Console.WriteLine($"{DateTime.Now:HH:mm:ss} {c}");
});
- 打开设置文件
appsettings.json
,修改数据库连接,模板默认提供如下连接
"ConnString": "Data Source=..\\Sample.db"
"ConnString": "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Sample;Jet OLEDB:Database Password=xxx;"
"ConnString": "Data Source=localhost;Initial Catalog=Sample;User Id=xxx;Password=xxx;"
"ConnString": "Server=(localdb)\\MSSQLLocalDB;Database=Sample;Trusted_Connection=True;"
"ConnString": "Data Source=localhost;port=3306;Initial Catalog=Sample;user id=xxx;password=xxx;Charset=utf8;SslMode=none;AllowZeroDateTime=True;"
"ConnString": "Host=localhost;Port=5432;Database=Sample;Username=xxx;Password=xxx;"
"ConnString": "Server=localhost;Schema=Sample;DATABASE=Sample;uid=xxx;pwd=xxx;"