EF Core Migrations with DbContext in Separate Library

Recently, in one of my attempt to write an API application in .Net core with Entity Framework Core (Code First) approach, I got ran into a couple of errors.

When I was looking for articles which can help me to get these issues resolved, all the articles were referring to the samples where the DbContext was created in the startup project instead of the separate assembly.

In my sample, the Entity Framework models were defined in the separate class library. Hence those articles were not of very great help for me to resolve this error.

After spending a lot of time, I got to know the solution. This article is to cover what were those errors and how to resolve them.

For making it easier to understand, you can refer the source code – MyCodeCamp – published in my github repository.

If you are using .NET Core 3, then please refer my other blog article. This article explains how to make dotnet ef migrations command work.

The Problem

I created two projects :

  • MyCodeCamp – A web application project for hosting the APIs
  • MyCodeCamp.Data – A class library project where all the EF Core entity models are placed.

The “MyCodeCamp.Data” project will contain only the entity models which are corresponding to the tables which are required to be created in database. It will also contain the CampContext which is derived from DbContext.

First Error

Then I tried to create the initial migration by using below command.

dotnet ef migrations add First 
Please note that these migration commands used in this article are executed on command prompt using .Net Core CLI.  These commands are executed inside the class library project  folder.

After running this command I got below error suggesting that the project cannot be executed directly. So the error was suggesting to either change the project type OR to specify –startup-project .

Startup project ‘MyCodeCamp.Data.csproj’ targets framework ‘.NETStandard’. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core .NET Command-line Tools with this project, add an executable project targeting .NET Core or .NET Framework that references this project, and set it as the startup project using –startup-project; or, update this project to cross-target .NET Core or .NET Framework. For more information on using the EF Core Tools with .NET Standard projects, see https://go.microsoft.com/fwlink/?linkid=2034781

Second Error

When I specified –startup-project and specified MyCodeCamp csproj file path as a value of that switch, I got another error (shown in red font):

dotnet ef migrations add First --startup-project ../MyCodeCamp.csproj

Unable to retrieve project metadata. Ensure it’s an MSBuild-based .NET Core project. If you’re using custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the –msbuildprojectextensionspath option.

Errors while generating migrations

The Solution

After spending a lot of time, I understood, that somehow this class library should have some implementation which can be used for identifying the additional details about generating migrations.

I fixed this issue using below two steps:

Implement IDesignTimeDbContextFactory

This factory can be used to create instances of the derived DbContext classes.

public class CampContextFactory : IDesignTimeDbContextFactory
{
   public CampContext CreateDbContext(string[] args)
   {
      var configuration = new ConfigurationBuilder()
           .SetBasePath(Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json")
           .Build();

      var dbContextBuilder = new DbContextOptionsBuilder();

      var connectionString = configuration
                  .GetConnectionString("SqlConnectionString");

      dbContextBuilder.UseSqlServer(connectionString);

      return new CampContext(dbContextBuilder.Options);
    }
}

Add AppSettings.json in the Class Library

Add appsettings.json file in the class library. Change the properties of this file so that it gets copied to the output directory. This file can contain the connection string.

"ConnectionStrings": {
    "SqlConnectionString" : "Data Source=.\\SQLExpress;Initial Catalog=MyCodeCampDb; Integrated Security=True;"
  },

Let’s apply migrations now !

Then I used below command to add the migrations. Of course, I had to specify the –startup-project switch and my startup project name.


dotnet ef migrations add First --startup-project ..\MyCodeCamp

Migrations were created successfully.

Let’s create database in SQL Server

Below command can be used to apply the migrations and create database in SQL Server.


dotnet ef database update --startup-project ..\MyCodeCamp

I hope you enjoyed reading this article and provided you sufficient information on resolving these errors. Please do comment and let me know your comments.

3 thoughts on “EF Core Migrations with DbContext in Separate Library

Add yours

  1. Tried this, but still getting:

    DbContext was found in assembly ‘FeynmanGroup.Common.Authorization.Functions’. Ensure that you’re using the correct assembly and that the type is neither abstract nor generic.

    I have 3 projects:

    FeynmanGroup.Common.Authorization.Functions – The main app. .net core 3.1
    FeynmanGroup.Repositories.Server.Authorization – Contains the DbContext. .net core 3.1
    FeynmanGroup.Repositories.Client.Authorization.DataModel – Contains the data model. .net standard 2.1

    Any idea?

    Like

  2. Nice post. for “IDesignTimeDbContextFactory” part. you don’t need “appsettings.json” , you may simply put connection string in the code. and put this method in #ifdef debug to strip it out of release build.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

Create your website at WordPress.com
Get started
%d bloggers like this: