应无所住,而生其心
排名
1
文章
860
粉丝
112
评论
163
net core webapi post传递参数
庸人 : 确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

abp vnext连接mysql。.net core连接mysql。ef core连接mysql

6139人阅读 2022/5/23 11:45 总访问:5182564 评论:0 收藏:0 手机
分类: ABP

增加一个MySQL的库:Volo.Abp.EntityFrameworkCore.MySQL

  1. <PackageReference Include="Volo.Abp.EntityFrameworkCore.MySQL" Version="4.4.2" />

然后把UseSqlServer换成UseMySQL

在把上下文对象中的连接字符串修改以下

以为这种就行了?那太天真了


执行迁移命令马上报错:Unable to create an object of type ‘DbContext’. For the different patterns supported at design time。

连接MySQL需要自己重写以下IDesignTimeDbContextFactory接口,配置一点逻辑

  1. public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<WyJBLandDbContext>
  2. {
  3. public WyJBLandDbContext CreateDbContext(string[] args)
  4. {
  5. var optionsBuilder = new DbContextOptionsBuilder<WyJBLandDbContext>();
  6. optionsBuilder.UseMySql(ServerVersion.AutoDetect("你的连接字符串"));
  7. return new WyJBLandDbContext(optionsBuilder.Options);
  8. }
  9. }

但是如果只是上边那样,虽然执行Add-Migration InitialCreate不会报错,能够正常生成迁移命令但是执行Update-Database就会报错了。


“A relational store has been configured without specifying either the DbConnection or connection string to use.”

只传递一个参数虽然执行迁移命令不会报错,但是执行Update-Database就会报错了。所以需要调用两个参数的方法。
解决方案
在UseMysql上,要传递两个参数(connectionstring,ServerVersion)

贴一下完整的代码,包括读取配置文件的代码

  1. /* This class is needed for EF Core console commands
  2. * (like Add-Migration and Update-Database commands) */
  3. public class WyJBLandDbContextFactory : IDesignTimeDbContextFactory<WyJBLandDbContext>
  4. {
  5. public WyJBLandDbContext CreateDbContext(string[] args)
  6. {
  7. //BookStoreEfCoreEntityExtensionMappings.Configure();
  8. var configuration = BuildConfiguration();
  9. string conn = configuration.GetConnectionString("conn_mysql");
  10. AddTestLog(conn);
  11. var builder = new DbContextOptionsBuilder<WyJBLandDbContext>()
  12. .UseMySql(conn,ServerVersion.AutoDetect(conn));
  13. return new WyJBLandDbContext(builder.Options);
  14. }
  15. /// <summary>
  16. /// 记录一点内容用于测试连接字符串是否正确获取到
  17. /// </summary>
  18. /// <param name="content"></param>
  19. public void AddTestLog(string content)
  20. {
  21. using (FileStream filestraem = new FileStream("d:/log.txt", FileMode.Append))
  22. {
  23. using (StreamWriter write = new StreamWriter(filestraem))
  24. {
  25. write.WriteLine(content);
  26. write.WriteLine("记录时间:" + DateTime.Now.ToString());
  27. write.WriteLine("----------------------------------------");
  28. write.Flush();
  29. }
  30. }
  31. }
  32. private static IConfigurationRoot BuildConfiguration()
  33. {
  34. var builder = new ConfigurationBuilder()
  35. .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../WY.JBLand.API/"))
  36. .AddJsonFile("appsettings.json", optional: true);
  37. return builder.Build();
  38. }
  39. }


还要注意以下mysql连接字符串不能使用.哦,要使用localhost代替否者会报错:
Unable to connect to any of the specified MySQL hosts.


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

EF多种更新方法。EF修改,Entity Framework修改。abp vnext ef 更新封装

[TOC] 方法1:直接执行更新的sql语句过于简单不说了 方法2:先查询在更新Users result = oapEntities.Users.Where(a =&gt...

EF添加。Entity Framework添加。abp vnext ef 添加,批量添加的封装

EF的添加如下,代码比较添加: ShipEntities se = new ShipEntities(); se.Users.Add(user); se.SaveChanges(); 方法2:...

领域驱动设计DDD abp vnext 一:项目架构搭建,模块使用

[TOC]用户接口层改造nuget中下载abp依赖VoLo.Abp.AspNetCore.Mvc ItemGroup添加方式 &lt;ItemGroup&gt; &lt;Packa...

abp vnext ObjectMapper 为空

abp vnext ObjectMapper 报空引用的错。System.NullReferenceException:“Object reference not set to an instance of an ...

领域驱动设计DDD abp vnext 二:使用仓储

[TOC]领域驱动设计仓储介绍在领域层和数据映射层之间进行中介,使用类似集合的接口来操作领域对象.” (Martin Fowler)。 实...

abp vnext 通用仓储,操作空引用。abp vnext 仓储报错,仓储为空。依赖注入报错

An internal error occurred during your request! abp vnext 通用仓储依赖注入拿到为空,或者能正常拿到依赖注入操作空引...

领域驱动设计DDD abp vnext 三:领域模型 之 失血模型,贫血模型,充血模型,胀血模型

[TOC]领域模型分为:失血模型,贫血模型,充血模型,胀血模型。 一、失血模型传统的三层架构,实体对象就是简单的POJO或者...

abp vnext ef core连接mysql报空引用错,连接SqlServer报空引用错。abp vnext 的坑

使用abp vnext 真的太喜欢报空引用的错了。你报点错给一点具体的错不行么,这样搞起让人去猜太坑了,比如前面的auto_fac没...

abp vnext项目结构分析

Domain 项目领域层,领域驱动开发的核心层。 它主要包含 实体, 集合根, 领域服务, 值类型, 仓储接口 和解决方案的其他领域...

abp vnext 实体的guid主键

Guid主键的实体如果你的实体Id类型为 Guid,有一些好的实践可以实现: 创建一个构造函数,获取ID作为参数传递给基类.— 如...

abp vnext 获取配置文件

代码如下: public override void ConfigureServices(ServiceConfigurationContext context) { var services = cont...

abp vnext验证

验证DTO微软官方文档:https://docs.microsoft.com/zh-cn/aspnet/core/mvc/models/validation?view=aspnetcore-7.0 数据注...

abp vnext 通用仓储 ef core。WhereIf,多条件,动态条件,分页,动态排序等

abp vnext 通用仓储 ef core WhereIf,多条件,分页等public async Task&lt;List&lt;LandInfoDto&gt;&gt; GetListAsync(Pag...

abp vnext 事务,多表查询,自定义仓储

[TOC]abp vnext 中的通用仓储主要是封装的单表操作,如果是实现事务的话最好还是封装一个自定义仓储。 自定义仓储大概的结...

abp vnext导航属性,级联查询。abp vnext 通用仓储 级联查询不到数据。abp vnext 多表查询,join

使用abp vnext的导航属性做级联查询,当然你可以使用自定义仓储,直接用原生的ef来写,也很简单方便abp vnext自定义仓储里...