Creating a shared EF Core database project for use in WPF and Xamarin.Forms apps

The basic idea

The way I go about this is I create a basic database context that only contains the DbSet properties and then make one more context per database provider (PostgreSQL, SQLite in this case) that derives from the basic one. The derived contexts then override their OnConfiguring methods where they set up the connection for their specific provider using a connection string extracted from a helper class.

Check out the example project

Check out the repository with the complete solution at https://github.com/michaldivis/ocean

Solution structure

Data (.NET Standard class library)

This project will contain the EF Core database context, data models, and some helpers.

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Tools
  • Npgsql.EntityFrameworkCore.PostgreSQL
  • Microsoft.EntityFrameworkCore.Sqlite

Logic (.NET Standard class library)

This project will contain a simple view model that will be shared by the other apps. This will be referencing the Data project.

  • Data
  • Microsoft.EntityFrameworkCore

DesktopApp (.NET Core WPF)

An example WPF app that will use PostgreSQL as the database. This will be referencing the Data and Logic projects.

  • Data
  • Logic
  • Microsoft.EntityFrameworkCore
  • Npgsql.EntityFrameworkCore.PostgreSQL

MobileApp (Xamarin.Forms)

An example mobile app that will use SQLite as the database. This will be referencing the Data and Logic projects.

  • Data
  • Logic
  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Sqlite

Used versions

SDK

  • .NET Core 3.1
  • .NET Standard 2.0
  • Xamarin.Forms 4.8

Nuget packages

  • Microsoft.EntityFrameworkCore 3.1.7
  • Npgsql.EntityFrameworkCore.PostgreSQL 3.1.4
  • Microsoft.EntityFrameworkCore.Sqlite 3.1.7

Creating the data project — contexts, models, helpers

Folders

I’ve created two folders in the data project

  • Config — migration configuration values will be here
  • Contexts — db contexts for different platforms will be here
  • Helpers — helper classes for making this usable on multiple platforms will be here
  • Migrations
  • — PostgreSqlMigrations — Migrations for PostgreSQL will be here
  • — SqliteMigrations — Migrations for SQLite will be here
  • Models — data model classes will be here

Models

Let’s say we want to have a database of fish. Here’s a class called Fish.

A way to use different database contexts for different platforms

Since we want to use different databases (PostgreSQL, SQLite) for the two apps, we’ll need a way to make the Data project take in some configuration (wanted database provider, connection string).

Database contexts

To use this on multiple platforms, we need to have different database contexts for each platform. However, they need to share the same data sets.

Almost ready, let’s migrate

Now before we use the app, there’s one last step — migrations.

Change the Data project’s target framework to multiple targets to run migrations with it

Go to the Data.csproj and change this:

<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netcoreapp2.0;netstandard2.0</TargetFrameworks>

Migrate

Open the Package Manager Console (Tools -> Nuget Package Manager -> Package Manager Console in Visual Studio 2019)

Configuring the connection for each platform

MobileApp project

Now let’s configure the database from the mobile project.

Configuring on start

To configure the database connection before we need to use, we’ll make all the call in the App.xaml.cs file.

DesktopApp project

Now let’s configure the database from the desktop project.

Let’s try it

Basic view model

To try this, I’ll create a simple class called MainViewModel that will serve as a view model for both the DesktopApp and the MobileApp.

Desktop app view

I’ve added a reference to the Logic project and edited the MainWindow to look like this:

Mobile app view

I’ve added a reference to the Logic project and edited the MainPage to look like this:

That’s it

Woohoo! That’s it.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store