Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating migrations for entities without a namespace produces an empty using statement #3313

Open
armanossiloko opened this issue Oct 12, 2024 · 1 comment
Assignees
Labels
bug Something isn't working
Milestone

Comments

@armanossiloko
Copy link

After creating a DbContext and an entity within the Program.cs file, calling add-migration produces a migration file with syntax error:
image

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>
@roji roji self-assigned this Oct 13, 2024
@roji roji added the bug Something isn't working label Oct 13, 2024
@roji
Copy link
Member

roji commented Oct 13, 2024

Confirmed, minimal repro:

var context = new MyDbContext();

public class Address
{
    public int Id { get; set; }
    public int PersonId { get; set; }
    public Metadata Metadata { get; set; }
}

public class Metadata
{
    public int Age { get; set; }
    public string? Interest { get; set; }
}

public class MyDbContext : DbContext
{
    public DbSet<Address> Addresses { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseNpgsql("Host=localhost;Username=test;Password=test");

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<Address>()
            .Property(e => e.Metadata)
            .HasColumnType("jsonb");
    }
}

@roji roji added this to the 9.0.0 milestone Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants