Trying to create a migration with dotnet-ef migrations add Initial
Build started...
Build succeeded.
Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[CloudBeholder.Identity.Infrastructure.ApplicationDbContext]' while attempting to activate 'CloudBeholder.Identity.Infrastructure.ApplicationDbContext'.' 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 am using SQLite. The packages related and their versions are in csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="8.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
</ItemGroup>
</Project>
My ApplicationDbContext and InfrastructureExtensionCollection bellow:
public class ApplicationDbContext : IdentityDbContext<IdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
public static class InfrastructureExtensionCollection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection service)
{
service.AddDbContext<ApplicationDbContext>(o => o.UseSqlite("Data Source=App.db"));
service.AddAuthorization();
service.AddIdentityApiEndpoints<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
return service;
}
}
In fact, I confused the name of two packages that have very similar spellings, they are Microsoft.EntityFrameworkCore.Sqlite.Core with
Microsoft.EntityFrameworkCore.Sqlite. After replacing withMicrosoft.EntityFrameworkCore.Sqlite.Coreand running the migration, everything worked as expected.