Furion多数据库添加

基于Furion 本身项目有个 EFCore连接MSSQL

现在需要连接MySQL 取数据

步骤一:添加 数据库上下文定位器 MySqlDbContextLocator

建议放 Furion.Core 项目

cs 复制代码
 /// <summary>
 /// 数据库上下文定位器
 /// </summary>
 public sealed class MySqlDbContextLocator : IDbContextLocator
 {

 }

步骤二:添加数据库上下文,且和 定位器 绑定

建议放 Furion.EntityFramework.Core / DbContexts /MySqlDbContext.cs

cs 复制代码
    [AppDbContext("MysqlConnectionString",DbProvider.MySql)]
    public class MySqlDbContext : AppDbContext<MySqlDbContext, MySqlDbContextLocator>
    {
        public MySqlDbContext(DbContextOptions<MySqlDbContext> options) : base(options)
        {
        }
    }

步骤三: 添加entity(表类) 要继承 : IEntity<MySqlDbContextLocator>

建议放 Furion.Core / Entities

cs 复制代码
 public class i18n_locales : IEntity<MySqlDbContextLocator>
 {
     public int ID { get; set; }
     public int created_by { get; set; }
     public string name { get; set; }
     public string code { get; set; }
 }

步骤四:配置连接字符串

如果你是web 建议放 : Furion/ samples / Furion.Web.Entry / appsettings.json

步骤五 :将MySQL连接 添加数据库池

建议放 Furion.EntityFramework.Core/startup. cs

cs 复制代码
  public class Startup : AppStartup
  {
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddDatabaseAccessor(options =>
          {
              options.CustomizeMultiTenants();
              options.AddDbPool<MySqlDbContext, MySqlDbContextLocator>(DbProvider.MySql);
              options.AddDbPool<DefaultDbContext>(DbProvider.SqlServer);
          }, "JoyAdmin.Database.Migrations");
      }
  }

**步骤六 :**Service 程序调用

建议放 Furion.Application

cs 复制代码
 public class RBACService : IDynamicApiController
 {
     private readonly IHttpContextAccessor _httpContextAccessor;
     private readonly IRepository<User> _userRepository;
     private readonly IRepository<EssConfig> _essRepository; 
    // 注意这里的类型
     private readonly IRepository<i18n_locales, MySqlDbContextLocator> _DeviceNoClassRepository; 
public RBACService(IHttpContextAccessor httpContextAccessor
    , IRepository<User> userRepository
    , IRepository<EssConfig> essRepository
    , IRepository<i18n_locales, MySqlDbContextLocator> DeviceNoClassRepository
    )
{
    _httpContextAccessor = httpContextAccessor;
    _userRepository = userRepository;
    _essRepository = essRepository;

    _DeviceNoClassRepository = DeviceNoClassRepository;
}
cs 复制代码
/// <summary>
/// 获取设备号列表
/// </summary>
/// <returns></returns>
public List<i18n_locales> GetListDeviceNo()
{
    var list = _DeviceNoClassRepository.AsQueryable().Adapt<List<i18n_locales>>();
    return list;
}

到此结束 但可能还有问题

问题一:Cannot create a DbSet for 'i18n_locales' because this type is not included in the model for the context.

无法为"i18n_locales"创建GbSet,因为该类型不包括在上下文的模型中。

因为 IRepository<EssConfig> 默认是继承 默认仓储 肯定没有mysql表里面的

在 RBACService中

, IRepository<EssConfig> essRepository

, IRepository<sqlmapoutput, MySqlDbContextLocator> DeviceNoClassRepository

问题二:The dbcontext locator `MySqlDbContextLocator` is not bind.

dbcontext定位器' MyMQlGbContextHandler '未绑定。

配置数据库连接的时候 指明 定位器

options.AddDbPool<MySqlDbContext, MySqlDbContextLocator>(DbProvider.MySql);

options.AddDbPool<DefaultDbContext>(DbProvider.SqlServer);

问题三:实体类型"i18n_locales"需要定义一个主密钥。如果您打算使用无键实体类型,请在"OnModel Creating"中调用"HasNoKey"。

创建的 entity 必须有一个以 ID 命名的字段

相关推荐
老华带你飞1 小时前
旅游|基于Java旅游信息系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·旅游
释怀°Believe1 小时前
javaweb
数据库·sql·oracle
Clarence Liu1 小时前
redis学习 (1) 基础入门
数据库·redis·学习
天生励志1232 小时前
Redis 安装部署
数据库·redis·缓存
北半球的夜2 小时前
emoji 表情符号保存问题
数据库·oracle
清风6666662 小时前
基于单片机的智能家居多参数环境监测与联动报警系统设计
数据库·单片机·毕业设计·智能家居·课程设计·期末大作业
煎蛋学姐2 小时前
SSM社区医院儿童预防接种管理系统84ui9(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架·儿童预防接种
锡兰_CC3 小时前
无缝触达,卓越体验:开启openEuler世界的任意门
服务器·网络·数据库·c++·图像处理·qt·nginx
ttthe_MOon3 小时前
MySQL 高可用解决方案 MHA:原理、配置与实践
数据库·mysql
一 乐3 小时前
高校评教|基于SpringBoot+vue高校学生评教系统 (源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习