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

相关推荐
面向星辰13 分钟前
sql基本增删改查语句汇总
数据库·sql·mybatis
Charles_go19 分钟前
C#13、什么是部分类
开发语言·c#
一氧化二氢.h1 小时前
MySQL root用户连接错误解决方法
android·数据库·mysql
q***23921 小时前
数据库操作与数据管理——Rust 与 SQLite 的集成
数据库·rust·sqlite
q***33371 小时前
给SQL server数据库表字段添加注释SQL,附修改、删除注释SQL及演示
数据库·sql·oracle
百***22121 小时前
mysql 迁移达梦数据库出现的 sql 语法问题 以及迁移方案
数据库·sql·mysql
_Jimmy_1 小时前
ShardingSphere-JDBC 实现两个mysql数据库的不同表的关联查询
数据库·mysql
weixin_307779132 小时前
基于AWS的应用程序可靠性提升架构优化方案——RDS多可用区与EC2弹性架构实践
数据库·数据仓库·架构·云计算·aws
Chan162 小时前
【 Java八股文面试 | Redis篇 缓存问题、持久化、分布式锁 】
java·数据库·redis·后端·spring·缓存·面试
G***T6912 小时前
PostgreSQL全文搜索教程,中文分词配置
数据库·postgresql·中文分词