C
C#9mo ago
sepp

✅ Layered architecture EF Core error when creating migrations

I have a Web Api project which is using .NET Core 7, my layers are DAL, Services Entities and API. I recently moved connection string to appsettings.json file (it was hardcoded in my appdbcondext class before) and created a extension method in DAL layer which sets connection string, I am using this in my startup project (API) program.cs but when I run dotnet ef migrations add testMig it gives an error
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
It was not happening when I hard code connection string in DAL layer and add/update migrations, ApplicationDbContext.cs (DAL)
namespace MeetingAppDemo.DAL.Context
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public DbSet<User> Users { get; set; }
public DbSet<Meeting> Meetings { get; set; }
public DbSet<MeetingParticipant> MeetingParticipants { get; set; }
public DbSet<MeetingDocument> MeetingDocuments { get; set; }
}
}
namespace MeetingAppDemo.DAL.Context
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public DbSet<User> Users { get; set; }
public DbSet<Meeting> Meetings { get; set; }
public DbSet<MeetingParticipant> MeetingParticipants { get; set; }
public DbSet<MeetingDocument> MeetingDocuments { get; set; }
}
}
DataAccessExtension.cs (DAL)
namespace MeetingAppDemo.DAL.Extensions
{
public static class DataAccessExtensions
{
public static IServiceCollection SetupDbContext(this IServiceCollection services, string connectionString)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
return services;
}
}
}
namespace MeetingAppDemo.DAL.Extensions
{
public static class DataAccessExtensions
{
public static IServiceCollection SetupDbContext(this IServiceCollection services, string connectionString)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
return services;
}
}
}
Program.cs (API Layer)
// Database Configuration
var dbConnectionString = builder.Configuration.GetConnectionString("Development");
builder.Services.SetupDbContext(dbConnectionString);
// Database Configuration
var dbConnectionString = builder.Configuration.GetConnectionString("Development");
builder.Services.SetupDbContext(dbConnectionString);
6 Replies
Tvde1
Tvde19mo ago
when you run dotnet ef migrations ... you need to specify the EF project and the startup project. Usually:
dotnet ef migrations add MyMigration -s MyApplication.API -p MyApplication.Data
dotnet ef migrations add MyMigration -s MyApplication.API -p MyApplication.Data
sepp
sepp9mo ago
I have tried that but I get different error which says
Your startup project 'MeetingAppDemo.API' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.
Your startup project 'MeetingAppDemo.API' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.
I tried installing EFCore and Design in my API project but nothing changes
Tvde1
Tvde19mo ago
yea the API needs the EFCore Design packages. Can you share the csproj file of your API?
sepp
sepp9mo ago
Oddly after restarting the Visual Studio add migration worked Another question, is it okay to have ef core packages installed both dal and api layer?
Tvde1
Tvde19mo ago
yeah it's necessary unfortunately
sepp
sepp9mo ago
Oh alright then, I assume problem solved for now. Thanks for helping 😅