0

I have created two projects in ASP.NET Core 8:

1. Web API project which contains

  • Connection string in appsettings.json
  • DbContext registration along with connection string in Program.cs file

2. Class library project which contains:

  • Models
  • Context, registered in program.cs file in Web API project
  • All Entity Framework Core packages - Tools, Design, SqlServer

I have set these two projects as start in startupConfiguration. Web API project have a reference to the class library project.

And I'm running the add-migration mgnName command in the class library project.

I get an error:

Unable to create a 'DbContext' of type ''. The exception 'The configuration file 'appsettings.json' was not found and is not optional. The expected physical path was 'K:.NET CORe\FinalWebProject\FinalWebProject.Data\appsettings.json'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

I have also done the design-time Db Creation but the error won't go away.

Please help me if anyone can!

2
  • Since since your appsettings.json is in your Web API project, you most likely need to specify it as the startup project: dotnet ef --startup-project ../Project.Api.proj migrations add mgnName - examples and discussion here: stackoverflow.com/questions/38705694/… Commented Sep 1, 2024 at 4:04
  • I Already in my startup configuration set the both projects as startup, and in package manager console running the migration in data project Commented Sep 1, 2024 at 6:01

1 Answer 1

0

Update from OP:

I just wanted to not use any database related package in my API project for sake of isolation, but after some research I found the solution : which is to create a DbContextFactory for Creating Migrations. And the solution I was seeking is impossible, but there is workaround which is to create the factory for creating the instance of the dbcontext.

========================

I didn't reproduce your issue in my side. Please kindly compare your codes with mine and have a try.

Firstly, I created a new .net 8 web api project in my VS, and just like what you mentioned. I added connection string in appsettings.json.

"ConnectionStrings": {
  "DefaultConnection": "xx"
},

And added DbContext registion in Program.cs

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));

What you didn't mentioned in your question is about the nuget packages. Here's what I added in my web api project.

<ItemGroup>
  <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
  <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
 <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
 </PackageReference>
</ItemGroup>

Then I created the ClassLibrary project. In this project, I created Data folder and Models folder. And my ApplicationDbContext is like below.

namespace ClassLibrary1.Data
{
    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        public DbSet<MovieRelease> MovieRel { get; set; }

    }
}

This is my Model

namespace ClassLibrary1.Models
{
    public class MovieRelease
    {
        [Key]
        public int ReleaseId { get; set; }
        public int ReleaseName { get; set; }
    }
}

And it requires nuget packages:

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

This is my project structure and my test result.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for your input, but I already have tried the way you posted and it works also. I just wanted to not use any database related package in my API project for sake of isolation, but after some research I found the solution : which is to create a DbContextFactory for Creating Migrations. And the solution I was seeking is impossible, but there is workaround which is to create the factory for creating the instance of the dbcontext
Thank you for coming back and I will update your workaround above. @DigvijaySingh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.