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

相关推荐
一 乐2 分钟前
学习平台|基于java的移动学习平台系统小程序(源码+数据库+文档)
java·数据库·学习·小程序·论文·apache
QQVQQ...14 分钟前
Git常见操作
数据库·git
一 乐23 分钟前
点餐|基于java的电子点餐系统小程序(源码+数据库+文档)
java·开发语言·前端·数据库·小程序·论文
读心悦28 分钟前
vscode 链接数据库
数据库·ide·vscode
镭速38 分钟前
镭速如何高效使用SQLite数据库高速传输结构化数据文件传输
jvm·数据库·sqlite
酥暮沐1 小时前
MySQL基础作业五
数据库·mysql
u0112900641 小时前
一种全新的webapi框架C#webmvc初步介绍
开发语言·c#
喜欢猪猪2 小时前
系统架构师---介绍ER图
数据库
q567315232 小时前
Metacritic 网站中的游戏开发者和类型信息爬取
数据库·python·线性代数·游戏·sqlite
ellis19702 小时前
C#迭代器方法和yield用法
c#