I am at the end with my nerves, since around a month I cannot create any migration files anymore with EF Core 8. Those are the project references for EF Core I have in my *.csproj file which I use to add-migration.
It did work before, but not anymore. I had on my laptop a branch, with which I created a migration and even with that branch I can't create any migration files anymore.
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
What have I done already:
- Added
DesignTimeDbContextFactory - Checked if I have correctly configured
ApplicationDbContextand addedDbSet<>with the correct model - I have in
ApplicationDbContexta empty constructor ApplicationDbContextis registered inProgram.cs- Checked if DB connection is correct and if it is connecting
- Searched with Github Copilot for a solution and told me that there is no error.
- Deleted current migration files and Snapshot file, still did not work
Both work, I debugged them.
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
public ApplicationDbContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
base.OnConfiguring(optionsBuilder);
}
// Here are all the DbSet<> like this:
public DbSet<MyEntity> MyEntities => Set<MyEntity>();
}
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
return new ApplicationDbContext(optionsBuilder.Options);
}
}
UPDATE:
In order to get it to run, I have reallocated the ApplicationDbContext in its own project.
My luck is that I already had the Enities in a own project :)
Other wise I would had to reallocate them too :(
