`Sqlite:Autoincrement` is not generated for key column when the entity has owned property
- 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
Assessment
This issue has not been assessed yet.