dotnet / dotnet/efcore

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

Open
#34,608 2 comments 2 reactions 0 assignees View on GitHub
area-sqlite customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

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(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(type: "INTEGER", nullable: false),
Owned_OwnedField = table.Column(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(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Owned_OwnedField = table.Column(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(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`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.