#34608·efcore

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

Author: noelexCreated Sep 4, 2024Updated Sep 27, 2026
Labelscustomer-reportedarea-sqlite

Consider the following configuration:

csharp
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:

csharp
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:

csharp
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:

csharp
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