Search code examples
c#.net-coreconsole-application

Unable to create setup EF Core and create migrations in a new .NET Core console app


I am attempting to create a simple .NET Core console app that accesses a database through EF Core. No matter what I do I am unable to successfully setup my Program.cs to fulfill whatever is needed. I keep getting the error below when trying to run migrations:

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: The entry point exited without ever building an IHost. Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[TestConsoleApp.ApplicationDbContext]' while attempting to activate 'TestConsoleApp.ApplicationDbContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Here are the exact steps I am doing (along w/ my exact code) if anyone wants to try and reproduce. Please note this all works perfectly fine when creating a Web API from scratch using dotnet new webapi.

I am running the following command to create a new .NET Core 8 console app:

dotnet new console -n ConsoleApp

I installed the following NuGet packages:

<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.2" />

I then create an Entities folder and add a simple Product.cs entity to it.

I then create a Data folder and add an ApplicationDbContext to it that looks like this:

public class ApplicationDbContext
{
    public 

ApplicationDbContext(DbContextOptions options) : base(options) { }

    public DbSet<Product> Products { get; set; }
}

Then in my Program.cs I am doing the following:

var services = new ServiceCollection();

// Add DbContext configuration
services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseSqlite("Data Source=App.db");
});

// Build the service provider
using (var serviceProvider = services.BuildServiceProvider())
{
    // Resolve the DbContext from the service provider
    var dbContext = serviceProvider.GetRequiredService<ApplicationDbContext>();
    Console.WriteLine("Hello, World!");
}

I then attempt to run the following command from the root of my project:

dotnet ef migrations add InitialCreate -o Data/Migrations

The error message suggests that the application is unable to build an instance of the IHost or IWebHost. Maybe what I am doing in the Program.cs of the console app does not fulfill this?

I removed everything from Program.cs and added the following to the DbContext directly:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlite("Data Source=MyApp.db");
}

The migrations actually create now and things appear to be working. However, I am still getting a yellow warning/error saying: "An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: The entry point exited without ever building an IHost."

If I add Console.ReadLine(); to Program.cs when calling the dotnet ef add migrations command the migrations are created, the console app stays running fine, then when I close it using ctrl+c the same yellow warning/error appears. I am going to assume it is a harmless error.

I really wanted to be able to have my DbContext sit in a central location so a WebAPI or a ConsoleApp could access it. The problem here is that the WebAPI can fulfill the context options injection, but the console app can't.


Solution

  • For .Net core console app to work with Entity Framework Core console app needs to work with generic host from Microsoft.Extensions.Hosting.

    Example on how to implement generic host can be found on this blog post by David Federman