GithubHelp home page GithubHelp logo

masroore / ef-core-multiple-providers Goto Github PK

View Code? Open in Web Editor NEW

This project forked from khalidabuhakmeh/ef-core-multiple-providers

0.0 1.0 0.0 13 KB

EF Core 7 Multiple Database Providers Example (SQLite and Postgres)

C# 100.00%

ef-core-multiple-providers's Introduction

Multiple Database Providers with EF Core

If you need the same models and relationships across multiple database providers, well then, this is the repository for you.

The goal of this repository is to show how you can set up a solution to have one DbContext interface to multple providers. In this case, we'll be using our VehiclesContext to communicate with SQLite and PostgreSQL.

Prerequisites

  • .NET 7 (preview as of this repo creation)
  • Postgres instance or Docker container instance

Provider Switching

Provider switching happens in the host application. In this example, we're using a argument passed in at the start of the application lifetime. Since we're using the IConfiguration instance, we could also set this value using environment variables, JSON values, or through any other registered configuration provider.

builder.Services.AddDbContext<VehiclesContext>(options =>
{
    var provider = config.GetValue("provider", Sqlite.Name);

    if (provider == Sqlite.Name)
    {
        options.UseSqlite(
            config.GetConnectionString(Sqlite.Name)!,
            x => x.MigrationsAssembly(Sqlite.Assembly)
        );
    }

    if (provider == Postgres.Name) {
        options.UseNpgsql(
            config.GetConnectionString(Postgres.Name)!,
            x => x.MigrationsAssembly(Postgres.Assembly)
        );
    }
});

A fascinating side-effect is provider switching can also happen on each request, meaning you could jump back and forth between each database provider. Not sure why you'd do that, but you could in theory.

Commands

Commands are assumed to be running at the root of the solution directory, and you will need to adjust your paths depending on your solution and projects.

> dotnet ef migrations add <Migration Name> --project ./Migrations/BoxedSoftware.Sqlite -- --provider Sqlite

> dotnet ef migrations add <Migration Name> --project ./Migrations/BoxedSoftware.Postgres -- --provider Postgres

Projects, Projects, Projects

While most developers won't mind the additional projects, you will need to separate many aspects of your solution into additional projects. This sample includes a Host, Models, and one project for each database provider. That's a total of 4 projects.

Caveats

There are caveats with this approach, the most obvious being the need to use features that are implemented in all database providers. This potentially excludes the use of EF.Functions in your queries. To work around these limitations you could do the following:

  1. Create methods on the DbContext that are provider aware, switching logic based on the current provider.
  2. Use stored procedures that implement database specific functionality closer to the database. (Note, stored procedures are not supported in SQLite).
  3. Branching code paths in business code based on the provider, and maybe even preprocessor directives and different builds of your app.

These are all techniques to help you around certain limitations of each database provider, but ultimately you want to avoid these edge cases as much as possible to allow for the least amount of differences in code.

License

Copyright (c) 2022 Khalid Abuhakmeh

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

ef-core-multiple-providers's People

Contributors

khalidabuhakmeh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.