Very Easy To Migrate Entity Framework Core for Class Library

What is Entity Framework Core?

EF Core is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write. EF Core supports many database engines.

Because EF Core is a new code base, the presence of a feature in past releases does not mean that the feature is implemented in EF Core. For this reason, they have provided a list of what is implemented and what they plan to implement before the initial release of EF Core.

Very Easy To Migrate Entity Framework Core for Class Library

In this post I will show you how to migrate EF core for class library projects. We need to pretend our class library is actually a .NET Core app. The trickery begins by updating our class library’s project.json with the following updates:

The most important parts are the build options at the root level, the Microsoft.EntityFrameworkCore.Design package and theMicrosoft.EntityFrameworkCore.Tools tool.

This part of the workaround is straight from the horse’s mouth and can be found in the docs here:

You’ll also need to add a static void main() to complete the .NET Core app charade. Add an empty program.cs to your class library project.

Lastly, we need to let our fake app know how to create your DbContext. The tools would normally gather this information from your startup.cs, but since that would be a huge pain to implement, let’s cheat and create an IDbContextFactory instead.

Add the following class to your class library:

This is just a quick way to let the app know what data provider we’re using and what the connection string is. Just hardcode it, don’t bother with the configuration system, this is only temporary and only used at development time.

Rate this post