efcore.pg
efcore.pg copied to clipboard
Generating migrations for entities without a namespace produces an empty using statement
After creating a DbContext and an entity within the Program.cs
file, calling add-migration
produces a migration file with syntax error:
The produced migration looks like this and everything is okay in it (it was generated correctly) except for line 1 where it has an empty using
:
using ;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace WebApplication1.Migrations
{
/// <inheritdoc />
public partial class test : Migration
{
// protected override void Up() omitted for brevity
// protected override void Down() omitted for brevity
}
}
Program.cs:
```csharp
using Microsoft.EntityFrameworkCore;
using Npgsql;
var builder = WebApplication.CreateBuilder(args);
var dataSource = new NpgsqlDataSourceBuilder(builder.Configuration.GetConnectionString("Database"))
.EnableDynamicJson().Build();
builder.Services.AddDbContext<MyDbContext>(options => options.UseNpgsql(dataSource));
var app = builder.Build();
//app.MapGet("/", (MyDbContext context) => ...
//app.MapGet("/add", (MyDbContext context) => ...
//app.MapGet("/update", (MyDbContext context) => ...
app.Run();
public class Person
{
public int Id { get; set; }
public required string Name { get; set; }
public List<Address> Addresses { get; set; } = [];
}
public class Address
{
public int Id { get; set; }
public int PersonId { get; set; }
public virtual Person? Person { get; set; }
public Metadata Metadata { get; set; }
}
public class Metadata
{
public int Age { get; set; }
public string? Interest { get; set; }
}
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options)
{
public DbSet<Person> People { get; set; }
public DbSet<Address> Addresses { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Address>()
.Property(e => e.Metadata)
.HasColumnType("jsonb");
}
}
The project file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.8" />
</ItemGroup>
</Project>