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.