dotnetcore / osharp

OSharp是一个基于.Net6.0的快速开发框架,框架对 AspNetCore 的配置、依赖注入、日志、缓存、实体框架、Mvc(WebApi)、身份认证、功能权限、数据权限等模块进行更高一级的自动化封装,并规范了一套业务实现的代码结构与操作流程,使 .Net 框架更易于应用到实际项目开发中。
Apache License 2.0
2.77k stars 749 forks source link

MySql数据库跑起来的步骤 #30

Closed gmf520 closed 6 years ago

gmf520 commented 6 years ago

我使用MySql5.7试了一下,框架现在的代码(0.3.0-beta10)完全可以跑起来的,没有问题。步骤如下:

  1. 安装MySql5.7,要启用MySql的utf8编码,即my.ini文件中启用character-set-server=utf8配置,重启MySql服务。
  2. 将现有的SqlServerDefaultDbContextMigrationPackSqlServerDesignTimeDefaultDbContextFactory两个类文件复制一份,更名为MySqlDefaultDbContextMigrationPackMysqlDesignTimeDefaultDbContextFactory,将文件内容中的SqlServer都替换成MySql,因为这两套代码都是服务于同一个上下文类型DefaultDbContext的,迁移的时候会重复,所以要将SqlServerDefaultDbContextMigrationPackSqlServerDesignTimeDefaultDbContextFactory两个SqlServer的文件从项目中排除掉。
  3. 更改配置类appsettings.jsonappsettings.Development.json,确保没有指向上下文DefaultDbContext的SqlServer配置节点,注意是两个文件的SqlServer配置节点都要删掉,否则会报“数据上下文配置中存在多个配置节点指向同一个上下文类型”的错误,因为系统初始化时两个文件都会读取的。然后创建MySql的配置节点,示例如下:
      "MySql": {
        "DbContextTypeName": "OSharp.Entity.DefaultDbContext,OSharp.EntityFrameworkCore",
        "ConnectionString": "Server=127.0.0.1;UserId=root;Password=abcd123456;Database=osharpns.demo-dev;charset='utf8';Allow User Variables=True",
        "DatabaseType": "MySql",
        "LazyLoadingProxiesEnabled": true,
        "AuditEntityEnabled": true,
        "AutoMigrationEnabled": true
      }
  4. 然后,当然是通过Add-Migration XXXX 命令创建迁移记录了,然后应该就能跑起来。
gmf520 commented 5 years ago

更新 (2019/05/15)

步骤

  1. 安装MySql5.7,编码要求 utf8mb4,下面是一个可用的配置文件
    [mysqld]
    user=mysql
    character-set-server=utf8mb4
    default_authentication_plugin=mysql_native_password
    [client]
    default-character-set=utf8mb4
    [mysql]
    default-character-set=utf8mb4
  2. 更改配置类appsettings.json和appsettings.Development.json,确保没有指向上下文DefaultDbContext的SqlServer配置节点,注意是两个文件的SqlServer配置节点都要删掉,否则会报“数据上下文配置中存在多个配置节点指向同一个上下文类型”的错误,因为系统初始化时两个文件都会读取的。然后创建MySql的配置节点,示例如下:
      "MySql": {
        "DbContextTypeName": "OSharp.Entity.DefaultDbContext,OSharp.EntityFrameworkCore",
        "ConnectionString": "Server=127.0.0.1;UserId=root;Password=abcd123456;Database=osharpns.demo-dev;charset='utf8';Allow User Variables=True",
        "DatabaseType": "MySql",
        "LazyLoadingProxiesEnabled": true,
        "AuditEntityEnabled": true,
        "AutoMigrationEnabled": true
      }
  3. 在Web项目下Startups文件夹下,排除SqlServer 的 SqlServerDefaultDbContextMigrationPack.cs, SqlServerDesignTimeDefaultDbContextFactory 两个文件,添加MySqlDefaultDbContextMigrationPack.cs, MySqlDesignTimeDefaultDbContextFactory文件
  4. 删除Web工程下的Migrations文件夹,重新进行数据迁移
  5. 可以跑起来了
xiongjx commented 5 years ago

Oracle 11g 按照上文步骤,排除SqlServer 的 SqlServerDefaultDbContextMigrationPack.cs, SqlServerDesignTimeDefaultDbContextFactory 两个文件,添加OracleDefaultDbContextMigrationPack.cs, OracleDesignTimeDefaultDbContextFactory文件 image Add-Migration Init update-database 报错:Object reference not set to an instance of an object. image

gmf520 commented 5 years ago

@xiongjx 没用过Oracle,不是很清楚

Jalonly commented 5 years ago

OracleDesignTimeDefaultDbContextFactory.cs文件最下面有一个默认写的builder.UseSqlServer,改为builder.UseOracle,并且将页面中的SqlServer:替换为Oracle:,则不会再出现以上问题

xiongjx commented 5 years ago

OracleDesignTimeDefaultDbContextFactory.cs文件最下面有一个默认写的builder.UseSqlServer,改为builder.UseOracle,并且将页面中的SqlServer:替换为Oracle:,则不会再出现以上问题

OracleDesignTimeDefaultDbContextFactory.cs文件最下面原文就是builder.UseOracle,您说的“将页面中的SqlServer:替换为Oracle:”这句没看明白,能够提供个demo?

haods commented 5 years ago

更新(2019/05/15)

  • 版本:1.0.0

步骤

  1. 安装MySql5.7,编码要求utf8mb4,下面是一个可用的配置文件
[mysqld]
user=mysql
character-set-server=utf8mb4
default_authentication_plugin=mysql_native_password
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
  1. 更改配置类appsettings.json和appsettings.Development.json,确保没有指向上下文DefaultDbContext的SqlServer的配置节点,注意是两个文件的SqlServer的配置节点都要删掉,否则会报“数据上下文配置中存在多个配置节点指向同一个上下文类型”的错误,因为系统初始化时两个文件都会读取的然后创建的MySql的配置节点,示例如下:
      "MySql": {
        "DbContextTypeName": "OSharp.Entity.DefaultDbContext,OSharp.EntityFrameworkCore",
        "ConnectionString": "Server=127.0.0.1;UserId=root;Password=abcd123456;Database=osharpns.demo-dev;charset='utf8';Allow User Variables=True",
        "DatabaseType": "MySql",
        "LazyLoadingProxiesEnabled": true,
        "AuditEntityEnabled": true,
        "AutoMigrationEnabled": true
      }
  1. 在Web项目下Startups文件夹下,排除SqlServer的SqlServerDefaultDbContextMigrationPack.csSqlServerDesignTimeDefaultDbContextFactory两个文件,添加MySqlDefaultDbContextMigrationPack.csMySqlDesignTimeDefaultDbContextFactory文件
  2. 删除网络工程下的迁移文件夹,重新进行数据迁移
  3. 可以跑起来了

最后运行之前需要先在NuGet控制台选中Web项目运行“Add-Migration init”来创建迁移记录,之后再运行就可以了