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 天前
每日八股——Redis(1)
数据库·经验分享·redis·后端·缓存
唐宋元明清21881 天前
.NET 磁盘管理-技术方案选型
windows·c#·存储
码熔burning1 天前
MySQL 8.0 新特性爆笑盘点:从青铜到王者的骚操作都在这儿了!(万字详解,建议收藏)
数据库·mysql
故事不长丨1 天前
C#正则表达式完全攻略:从基础到实战的全场景应用指南
开发语言·正则表达式·c#·regex
猫头虎1 天前
2025最新OpenEuler系统安装MySQL的详细教程
linux·服务器·数据库·sql·mysql·macos·openeuler
哈库纳玛塔塔1 天前
放弃 MyBatis,拥抱新一代 Java 数据访问库
java·开发语言·数据库·mybatis·orm·dbvisitor
@LetsTGBot搜索引擎机器人1 天前
2025 Telegram 最新免费社工库机器人(LetsTG可[特殊字符])搭建指南(含 Python 脚本)
数据库·搜索引擎·机器人·开源·全文检索·facebook·twitter
计算机毕设VX:Fegn08951 天前
计算机毕业设计|基于springboot + vue动物园管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
冉冰学姐1 天前
SSM校园排球联赛管理系统y513u(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架应用·开题报告、
Tony Bai1 天前
【分布式系统】03 复制(上):“权威中心”的秩序 —— 主从架构、一致性与权衡
大数据·数据库·分布式·架构