ABP应用——修改EF Core

自从.net core跨平台后,.net环境下的开源项目渐渐丰富起来,ABP使用ef core作为数据库组件,并且使用code first,所以涉及到migration操作。

下载代码

ABP自定义模板下载

这次技术更新的重点之一是.net core,所以必须选ASP.Net Core 2.x。没深究过新的前端技术栈,先排除angular,vue,React,为了更好的上手,选择了Include login,register...,随便取个名字,create my project就下载到了源码。

运行程序 

官方Document

  • Open your solution in Visual Studio 2017 v15.3.5+ and build the solution.
  • Select the 'Web.Mvc' project as the startup project.
  • Check the connection string in the appsettings.json file of the Web.Mvc project, change it if you want.
  • Open Package Manager Console and run the Update-Database command to create your database (ensure that the Default project is selected as .EntityFrameworkCore in the Package Manager Console window).
  • Run the application.

如上所述,官方给出了运行程序的方法,当然前提是有SQL server数据库,看了一圈我的服务器并没有安装,还是一步到位直接换成mysql吧。

搜索一下,官方也给出了修改为mysql的方案,但是看起来像是给.net framework准备的,代码都不一样,放弃。

1. 移除EntityFrameworkCore项目下的EntityFrameworkCore.Design和EntityFrameworkCore.SqlServer

新增Pomelo.EntityFrameworkCore.MySql和Pomelo.EntityFrameworkCore.MySql.Design 

2.修改代码

\myABPSingle\aspnet-core\src\myABPSingle.EntityFrameworkCore\EntityFrameworkCore\myABPSingleDbContextConfigurer.cs

修改为:

namespace myABPSingle.EntityFrameworkCore
{
    public static class myABPSingleDbContextConfigurer
    {
        public static void Configure(DbContextOptionsBuilder<myABPSingleDbContext> builder, string connectionString)
        {
            //builder.UseSqlServer(connectionString);
            builder.UseMySql(connectionString);
        }

        public static void Configure(DbContextOptionsBuilder<myABPSingleDbContext> builder, DbConnection connection)
        {
            //builder.UseSqlServer(connection);
            builder.UseMySql(connection);
        }
    }
}

 

3.清空Migrations文件夹,自带的文件是用于SQL server的,一开始直接Update-Database到mysql,可以执行,但id字段不会自增,程序运行报错,所以全部删除。

 编译一遍

4.修改Web.MVC项目的连接字符串

打开Web.MVC项目appsettings.json,修改连接字符串,替换其中的星号

 "ConnectionStrings": {
    "Default": "Server=192.168.*.*;Port=3306;Database=abp;Uid=root;Pwd=******;SslMode=none;"
  }

5.生成migration文件

打开视图-其他窗口-程序包管理器控制台

 修改默认项目为EntityFrameworkCore,修改启动项目为EntityFrameworkCore

执行Add-Migration init_all

成功后会显示

PM> Add-Migration init_all
To undo this action, use Remove-Migration.

并且在Migrations文件夹下出现init_all.cs脚本

6.应用到数据库

在控制台运行Update-Database,查看数据库已经简表完毕

7.运行程序

admin:123qwe

参考文章:

ABP .Net Core Entity Framework迁移使用MySql数据库 

ABP 常见错误

EF MySql Integration

Startup Template Core

ABP学习:领域层创建实体(使用MySql数据库)

基于DDD的现代ASP.NET开发框架--ABP系列之2、ABP入门教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值