Home > front end >  .Net 6 EF Core DbContext and Models In Separate Class Libary
.Net 6 EF Core DbContext and Models In Separate Class Libary

Time:08-02

I have a solution with the following projects:

  • ASP.NET Core MVC website
  • ASP.NET Core Web API
  • DAL [incl DbContext and model classes]

Both the website and API reference the DAL and can use it's dbContext and model classes.

Both these projects have an appsettings.json file with the connection string and DB access is all working.

I can't how ever work out how to add migrations.

If I set the default project to the DAL project, I get this error:

Unable to create an object of type 'myDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

If I set it to the Api or website, I get:

Your target project 'website' doesn't match your migrations assembly 'DAL'. Either change your target project or change your migrations assembly.
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("website")). By default, the migrations assembly is the assembly containing the DbContext.
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.

I can find information on how to set up a separate class lib for the dbcontect and models for .NET Core 3.1 but I am struggling with .NET 6.

I have tried to follow this tutorial

https://garywoodfine.com/using-ef-core-in-a-separate-class-library-project/

But it's not the same as .net 6 so I cant follow it as I get errors when I try and create the DBContextFactory towards the bottom. The SetBasePath is not a definition within ConfigurationBuilder. I'm not fluent enough with .NET Core to work out whats required.

CodePudding user response:

I have the exact same setup as you have with a website, api and a DAL layer.

This is how I register the DbContext in the website and in the api program.cs:

builder.Services.AddDbContext<AppDbContext>(
    options => options.UseSqlServer(config.ConnectionString, sqlOptions => {
        sqlOptions.MigrationsAssembly("App.Entities");}));

This is how the AppDbContext is created in the DAL project:

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    { }

    // Your DbSet entities here
}

Then when you run your first migration command "Add-Migration" you need to select the DAL project in the package manager console dropdown and it should automatically create the migration folder with the migration files.

You need to have the following packages installed in the DAL project:

Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools

CodePudding user response:

Honestly, one would need more information to narrow down the problem.

I think there are two possible problems.

  1. You are not using the exact DB Context in the program.cs of the two project in the presentation layer(MVC and API).

  2. You have updated the migration the migration on API or MVC, or you have made changes to the database hence the comparison is not as clear.

How to resolve:

  • Make sure that database connection string in the two application.json is same

  • Make sure that in the Program.cs of the the API and the MVC, that you have added the exact dbcontext from DAL "and is identical".

  • Make sure you have uniform Nugget package across the solution for the EF tool, Server, Design

  • Make sure the Models in the last migration mirrors what is obtainable in the database.

  • Make sure that the Package Manager console is has DAL selected as the default project, while the startup project which can be MVC or API is not having any errors.

  • Make sure that there is a dbSet<> in the dbContext in DAL before adding a migration

  • Related