Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core Entity Framework - Add migration for Context in class library

I'm having problems adding an initial migration to my Entity Framework database context inside a .NET Core class library.

When I run:

dotnet ef migrations add migrationName -c PlaceholderContext

I get error:

Could not invoke this command on the startup project 'Placeholder.Data'. This version of the Entity Framework Core .NET Command Line Tools does not support commands on class library projects in ASP.NET Core and .NET Core applications. See http://go.microsoft.com/fwlink/?LinkId=798221 for details and workarounds.

So I clicked on the link and learned that it's not possible to add a migration to a class library. You can however convert the class library project into an "app" project but by doing so I can not reference this "app" project from my business layer (class library).

Project structure:

Placeholder.Web (WebAPI) => Placeholder.Business (class library) => Placeholder.Data (class library)

Project structure

Placeholder.Web => Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);

        services.AddMvc();

        //HERE WE REGISTER DB CONTEXT, (STATIC CLASS IN BUSINESS LAYER)
        services.InjectBusinessContext(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=Placeholder;Integrated Security=True;Connect Timeout=30;");

        services.InjectWebServices();
        services.InjectBusinessServices();
    }

How can I overcome this really annoying problem?

Update (1)

I have converted my Placeholder.Data class library to an "app" with a static main method. Because I can no longer reference Placeholder.Data from Placeholder.Business I have to go with workaround 2 listed on the microsoft doc page. When I now run the migration script I get the following:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext

Doh ofcourse this wont work, the dbcontext is registered from my Placeholder.Web app, (through business layer). Then my only option is to add a new context in the new static main method and I dont really want to do this..

like image 917
Reft Avatar asked Feb 13 '17 21:02

Reft


2 Answers

You don't need to 'convert' your data project to an app. Here is a test app with a similar structure:

project structure

In the project.json in the Data project, add the asp.net core nuget packages.

project.json

Now, to create a migration, just right click on the Data project, choose 'Open folder in file explorer,' then in file explorer, press Shift + right click and 'Open command window here.'

To create the migration, just specify the 'startup project' as the web app (where the startup.cs exists)

dotnet ef --startup-project ../TestPatterns2.Web migrations add Second

migrations

And voila, migration:

second migration

TO ADD THE MIGRATION FOLDER INTO THE DATA PROJECT: when you defined the service, add the migration point like so

services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("TestPatterns2.Data")));
like image 121
Reza Avatar answered Nov 02 '22 16:11

Reza


It only needs to be an app for making the migration (needs an entry point) so after making the library an app and creating your migration, comment out the buildOptions and runtimes elements in your project.json. It will now build as a library again.

Uncomment them whenever you need to add another migration.

like image 31
BradleyDotNET Avatar answered Nov 02 '22 14:11

BradleyDotNET