Skip to content

Sqlite:Autoincrement is not generated for key column when the entity has owned property #34608

Open
@noelex

Description

@noelex

Consider the following configuration:

public class SomeRandomEntity
{
    public long Id { get; set; }

    public OwnedEntity? Owned { get; set; }
}

public class OwnedEntity
{
    public int OwnedField { get; set; }
}

internal class ReproDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SomeRandomEntity>(entity =>
        {
            entity.HasKey(x => x.Id);
            entity.OwnsOne(x => x.Owned);
        });
    }
}

dotnet ef migrations add generates a migration with the following code:

migrationBuilder.CreateTable(
    name: "SomeRandomEntity",
    columns: table => new
    {
        Id = table.Column<long>(type: "INTEGER", nullable: false),
        Owned_OwnedField = table.Column<int>(type: "INTEGER", nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_SomeRandomEntity", x => x.Id);
    });

Note that Sqlite:Autoincrement is missing on Id. This can be 'fixed' by either changing the name of the entity to MyEntity:

migrationBuilder.CreateTable(
    name: "MyEntity",
    columns: table => new
    {
        Id = table.Column<long>(type: "INTEGER", nullable: false)
            .Annotation("Sqlite:Autoincrement", true),
        Owned_OwnedField = table.Column<int>(type: "INTEGER", nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_MyEntity", x => x.Id);
    });

or by removing Owned property:

migrationBuilder.CreateTable(
    name: "SomeRandomEntity",
    columns: table => new
    {
        Id = table.Column<long>(type: "INTEGER", nullable: false)
            .Annotation("Sqlite:Autoincrement", true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_SomeRandomEntity", x => x.Id);
    });

Reverting Microsoft.EntityFrameworkCore.Sqlite and Microsoft.EntityFrameworkCore.Design to 6.0.33 also fixes the issue.

EF Core version: 8.0.8
Database provider: Microsoft.EntityFrameworkCore.Sqlite
Target framework: net-8.0

Metadata

Metadata

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions