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 命名的字段

相关推荐
豆沙沙包?36 分钟前
2025年--Lc171--H175 .组合两个表(SQL)
数据库·sql
麋鹿原44 分钟前
Android Room 数据库之数据库升级
数据库·kotlin
GanGuaGua2 小时前
MySQL:表的约束
数据库·mysql
Li zlun3 小时前
MySQL 性能监控与安全管理完全指南
数据库·mysql·安全
William_cl4 小时前
C# MVC 修复DataTable时间排序以及中英文系统的时间筛选问题
开发语言·c#·mvc
养生技术人4 小时前
Oracle OCP认证考试题目详解082系列第48题
运维·数据库·sql·oracle·database·开闭原则·ocp
c#上位机4 小时前
wpf之RelativeSource用法总结
c#·wpf
海阳宜家电脑4 小时前
Lazarus使用TSQLQuery更新的一点技巧
数据库·lazarus·tsqlquery
Dm_dotnet5 小时前
WPF应用最小化到系统托盘
c#
丨我是张先生丨5 小时前
SQLSERVER 查找存储过程中某个变量
数据库