Shubham Guha
Shubham Guha

Reputation: 3

Unable to perform migrations in .NET Core

The error which I am facing:

PM> update-database
Build started...
Build succeeded.

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider.
Error: Failed to load configuration from file 'C:\Users\shubh\source\repos\ProjectOne\appsettings.json'.

Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[ProjectOne.Data.ApplicationContext]' while attempting to activate 'ProjectOne.Data.ApplicationContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

ApplicationContext.cs:

using Microsoft.EntityFrameworkCore;
using ProjectOne.Models;

namespace ProjectOne.Data
{
    public class ApplicationContext : DbContext
    {
        public ApplicationContext(DbContextOptions<ApplicationContext> options):base(options) { }

        public DbSet<Employee> Employees { get; set; }
        public DbSet<Department> Departments { get; set; }
    }
}

appsettings.json:

{
  {
    "ConnectionStrings": {
      "ProductionDB": "Data Source=DESKTOP-R6LR1KA\\SQLEXPRESS;Initial Catalog=MultiTable;Integrated Security=True;Trust Server Certificate=True"
    },
    "Logging": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.AspNetCore": "Warning"
      }
    },
    "AllowedHosts": "*"
  }

Program.cs:

using Microsoft.EntityFrameworkCore;
using ProjectOne.Data;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("ProductionDB")));

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

I have installed Entity Framework Tools, EF Core, EF Design as well. It's still not working. I hope you can fix it! I am using EF Core v8.0.7

Upvotes: 0

Views: 47

Answers (1)

Ramshankar TV
Ramshankar TV

Reputation: 101

  1. There is extra curly brace in your json file. Make sure json format is correct.

  2. Make sure the appsettings.json is copied in your output folder.If not, refer this: https://stackoverflow.com/a/49781172/11993452

Upvotes: 1

Related Questions