根据微软官方文档的说法,有两种方法可以实现在一个app中同时适应多种不同类型的数据库,并且全部支持migrations操作。其一,使用两个dbcontext;其二,修改migration文件,添加特定数据库类型的Annotation。

本人在实际使用过程中发现,第二种情况几乎很难调通,总是在migration操作时遭遇各种奇怪问题,而且这种操作似乎也不太”干净“;第一种操作,一开始无论如何也调不通,后来发现是dbcontext的注入方式有问题,不能像默认模板生成的那样使用options参数,而是应该使用空白的构造方法,具体代码如下:

DbContext:

    public class ApplicationDbContext : IdentityDbContext
{
public DbSet<Location> Locations { get; set; } protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
} public class PgDbContext : ApplicationDbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseNpgsql(Startup.Configuration.GetConnectionString("DefaultConnection"));
}
} public class MsDbContext : ApplicationDbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer(Startup.Configuration.GetConnectionString("DefaultConnection"));
}
}

注意覆盖的方法是OnConfiguring。

然后就是Startup中根据配置的数据库类型注入不同的dbcontext:

        public void ConfigureServices(IServiceCollection services)
{
var dbType = Configuration.GetValue<string>("DbType"); //多种数据库支持
if (dbType == "PGSQL")
services.AddDbContext<ApplicationDbContext, PgDbContext>();
else
services.AddDbContext<ApplicationDbContext, MsDbContext>(); services.AddDefaultIdentity<IdentityUser>(options => {
options.SignIn.RequireConfirmedAccount = false;
}).AddEntityFrameworkStores<ApplicationDbContext>(); services.AddControllersWithViews();
services.AddRazorPages();
}

然后,别忘了,给不同的数据库类型(dbcontext)建立不同的migration:

Add-Migration Initial -Context MsDbContext -OutputDir Migrations/Mssql

Add-Migration Initial -Context PgDbContext -OutputDir Migrations/Pssql

以上命令分别建立不同的migrations。

最后,在执行context.Database.Migrate()时,context就会根据数据库类型自动寻找正确的migrations了。

关键:不再通过options参数构造dbcontext。

asp.net core + entity framework core 多数据库类型支持实战的更多相关文章

  1. ABP 教程文档 1-1 手把手引进门之 ASP.NET Core & Entity Framework Core(官方教程翻译版 版本3.2.5)

    本文是ABP官方文档翻译版,翻译基于 3.2.5 版本 官方文档分四部分 一. 教程文档 二.ABP 框架 三.zero 模块 四.其他(中文翻译资源) 本篇是第一部分的第一篇. 第一部分分三篇 1- ...

  2. NET Core & Entity Framework Core

    ABP 教程文档 1-1 手把手引进门之 ASP.NET Core & Entity Framework Core(官方教程翻译版 版本3.2.5)   本文是ABP官方文档翻译版,翻译基于 ...

  3. ASP.NET Core 入门教程 8、ASP.NET Core + Entity Framework Core 数据访问入门

    一.前言 1.本教程主要内容 ASP.NET Core MVC 集成 EF Core 介绍&操作步骤 ASP.NET Core MVC 使用 EF Core + Linq to Entity ...

  4. ASP.NET Core 入门笔记9,ASP.NET Core + Entity Framework Core 数据访问入门

    一.前言 1.本教程主要内容 ASP.NET Core MVC 集成 EF Core 介绍&操作步骤 ASP.NET Core MVC 使用 EF Core + Linq to Entity ...

  5. Asp.net Mvc Entity Framework Code First 数据库迁移

    1.创建Mvc项目 2.安装Entity Framework 2.1.如下图打开程序包管理器控制台: 2.2.输入命令Install-Package EntityFramework,即可安装Entit ...

  6. 尝试.Net Core—使用.Net Core + Entity FrameWork Core构建WebAPI(一)

    想尝试.Net Core很久了,一直没有时间,今天回家,抛开一切,先搭建一个.Net Core的Demo出来玩玩. 废话少说,咱直奔主题: 一.开发环境 VS2015 Update3 Microsof ...

  7. .net core Entity Framework Core Code First 框架 分层开发

    由于之前苦于无法把 Entityframework 跟Web层剥离.找了很久..找到了这个框架..分享给大家..  GitHub 地址:https://github.com/chsakell/dotn ...

  8. ASP.NET Core 配置 Entity Framework Core - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 配置 Entity Framework Core - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 配置 Entity Fram ...

  9. 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表

    创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...

随机推荐

  1. vue中的错误日志

    一.Error compiling template: Component template requires a root element, rather than just text. 这个错误意 ...

  2. Bi-shoe and Phi-shoe LightOJ - 1370

    欧拉函数. 欧拉函数打表模板: #define maxn 3000010 int p[maxn]; void oula(){ int i,j; ; i<=maxn; i++) p[i]=i; ; ...

  3. Linux学习笔记(五)搜索命令

    搜索命令 whereis which locate find whereis 英文原意:locate the binary,source,and manual page files for a com ...

  4. Flutter 步骤进度组件

    ​老孟导读:最近文章更新拖后腿了,一直忙着网站改版的事情,今天总算落地了,全新的Flutter网站即将上线,敬请期待.网站目前收集197个组件的详细用法,还有150多个组件待整理. Stepper S ...

  5. Python之学会测试,让开发更加高效(一)

      前几天,听了公司某位大佬关于编程心得的体会,其中讲到了"测试驱动开发",感觉自己的测试技能薄弱,因此,写下这篇文章,希望对测试能有个入门.这段时间,笔者也体会到了测试的价值,一 ...

  6. SpringCloud-Alibaba-Nacos 服务注册中心&配置中心

    Spring Cloud Alibaba 由于 Spring Cloud Netflix 项目进入维护模式(将模块置于维护模式意味着 Spring Cloud 团队将不会再向模块中添加新功能,只会修复 ...

  7. [Abp vNext 入坑分享] - 4.JWT授权的接入

    一.感想 在写这一系列文章之前,本来以为写这个之前已经搭建好的框架描述会比较简单,但是慢慢写下来才发现.写这个真的不简单额,本来以为图文一起,一个晚上应该能输出一篇吧...结果:现实真的骨感,一个星期 ...

  8. [SVN] Couldn't perform atomic initialization

    svn: Commit failed (details follow): svn: Couldn't perform atomic initialization It was because the ...

  9. Spring Boot JPA中关联表的使用

    文章目录 添加依赖 构建Entity 构建Repository 构建初始数据 测试 Spring Boot JPA中关联表的使用 本文中,我们会将会通过一个Book和Category的关联关系,来讲解 ...

  10. Django中search fields报错:related Field has invalid lookup: icontains

    models.py 文件 # coding:utf8from django.db import models class Book(models.Model):        name = model ...