Reputation: 5688
I'm using EF Core 2.2, using a code first approach.
I have my entity class:
public class Client
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int ClientID { get; set; }
public string Name { get; set; }
}
and I'm seeding my data like so:
var client = new Client { Name = "TestClient"};
modelBuilder.Entity<Client>().HasData(client);
But I receive an error when trying to add a migration:
The seed entity for entity type 'Client' cannot be added because a non-zero value is required for property 'ClientID'. Consider providing a negative value to avoid collisions with non-seed data.
The ClientID
should be automatically generated and I don't want to specify it. Is there a workaround to this or has this functionality simply not been implemented yet?
Upvotes: 10
Views: 8572
Reputation: 190897
With seed data, you must specify the key. Otherwise it won't know which record to ensure is there. It is documented here.
Upvotes: 15