ABP vNext切换到EF Core Oracle3.1提供程序
删除Volo.Abp.EntityFrameworkCore.SqlServer包
解决方案中的 .EntityFrameworkCore
项目删除 Volo.Abp.EntityFrameworkCore.SqlServer 包.。
nuget,设置默认项目:.EntityFrameworkCore
Install-Package Oracle.EntityFrameworkCore -Version 3.19.0-beta1
增加新的扩展类
因ABP没有封装Oracle的模块,需要自己扩展
我在直接在EntityFrameworkCore项目中增加对Oracle数据库的扩展
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using JetBrains.Annotations;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using Oracle.EntityFrameworkCore.Infrastructure;
using Volo.Abp.EntityFrameworkCore;
namespace Admin.EntityFrameworkCore
{
public static class AbpDbContextConfigurationContextOracleExtensions
{
public static DbContextOptionsBuilder UseOracle(
[NotNull] this AbpDbContextConfigurationContext context,
[CanBeNull] Action<OracleDbContextOptionsBuilder> oracleSQLOptionsAction = null)
{
if (context.ExistingConnection != null)
{
return context.DbContextOptions.UseOracle(context.ExistingConnection, oracleSQLOptionsAction);
}
else
{
return context.DbContextOptions.UseOracle(context.ConnectionString, oracleSQLOptionsAction);
}
}
}
public static class AbpDbContextOptionsMySQLExtensions
{
public static void UseOracle(
[NotNull] this AbpDbContextOptions options,
[CanBeNull] Action<OracleDbContextOptionsBuilder> oracleSQLOptionsAction = null)
{
options.Configure(context =>
{
context.UseOracle(oracleSQLOptionsAction);
});
}
public static void UseMySQL<TDbContext>(
[NotNull] this AbpDbContextOptions options,
[CanBeNull] Action<OracleDbContextOptionsBuilder> oracleSQLOptionsAction = null)
where TDbContext : AbpDbContext<TDbContext>
{
options.Configure<TDbContext>(context =>
{
context.UseOracle(oracleSQLOptionsAction);
});
}
}
}
//xxxxxxEntityFrameworkCoreModule.cs文件
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddAbpDbContext<SysAdminDbContext>(options =>
{
/* Remove "includeAllEntities: true" to create
* default repositories only for aggregate roots */
options.AddDefaultRepositories(includeAllEntities: true);
});
Configure<AbpDbContextOptions>(options =>
{
/* The main point to change your DBMS.
* See also SysAdminMigrationsDbContextFactory for EF Core tooling. */
options.UseOracle(b => b.UseOracleSQLCompatibility("11"));
});
}
查找你的解决方案中 UseSqlServer()
调用,替换为 UseOracle()
更改连接字符串
"ConnectionStrings": {
"Default": "DATA SOURCE=LOCALHOST:1521/ORACLE1;USER ID=sys ;PASSWORD=123;Persist Security Info=True;"
}
通常需要更改 .DbMigrator
和 .Web
项目里面的 appsettings.json
,但它取决于你的解决方案结构.
重新生成迁移
- 删除
.EntityFrameworkCore.DbMigrations
项目下的Migrations文件夹,并重新生成解决方案. - 在包管理控制台中运行
Add-Migration "Initial"
(在解决方案资源管理器选择.DbMigrator
(或.Web
) 做为启动项目并且选择.EntityFrameworkCore.DbMigrations
做为默认项目). - 需要手工在Oracle数据库中建立用户。
这将创建一个配置所有数据库对象(表)的数据库迁移.
运行 .DbMigrator
项目创建数据库和初始种子数据.
运行应用程序
IdentityServer、Host、Web项目。
abp2.8版本,oracle11g数据库,.net core 3.1 测试成功。