问题详情
323 | 清风大侠
如何更换框架默认数据库,只需三步操作即可更换

更换默认数据库,分三步走。

  • 打开后端Web工程文件,更换数据库提供者,模板默认提供如下提供者,只需选择其一
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.8" />
<PackageReference Include="System.Data.OleDb" Version="8.0.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
<PackageReference Include="MySqlConnector" Version="2.3.7" />
<PackageReference Include="Npgsql" Version="8.0.3" />
<PackageReference Include="DM.DmProvider" Version="8.3.1.25526" />
  • 打开后端Web工程Program.cs文件,更换框架默认数据库
builder.Services.AddApp(info =>
{
    info.Connections = [new Known.ConnectionInfo
    {
        Name = "Default", //框架默认的数据库连接名称,不要修改
        DatabaseType = DatabaseType.SQLite,
        ProviderType = typeof(Microsoft.Data.Sqlite.SqliteFactory),
        //DatabaseType = DatabaseType.Access,
        //ProviderType = typeof(System.Data.OleDb.OleDbFactory),
        //DatabaseType = DatabaseType.SqlServer,
        //ProviderType = typeof(System.Data.SqlClient.SqlClientFactory),
        //DatabaseType = DatabaseType.MySql,
        //ProviderType = typeof(MySqlConnector.MySqlConnectorFactory),
        //DatabaseType = DatabaseType.PgSql,
        //ProviderType = typeof(Npgsql.NpgsqlFactory),
        //DatabaseType = DatabaseType.DM,
        //ProviderType = typeof(Dm.DmClientFactory),
        ConnectionString = builder.Configuration.GetSection("ConnString").Get<string>()
    }];
    // 如果要添加业务数据库连接,在此添加一个 ConnectionInfo 到 info.Connections 集合里
});
  • 打开设置文件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;"
回复列表

暂无数据

晚上好!