When trying to add a migration, ef core gives me the following error:
The seed entity for entity type 'MyEntity' cannot be added because a non-zero value is required for property 'Id'. Consider providing a negative value to avoid collisions with non-seed data.
Entity MyEntity and its superclass/interface:
public class MyEntity: EntityObject
{
public long OrderId { get; set; }
public List<StateChange> StateChanges { get; set; }
}
public class EntityObject : IEntityObject
{
[Key]
public int Id { get; set; }
[Timestamp]
public byte[] RowVersion
{
get;
set;
}
}
public class IEntityObject
{
int Id { get; set;
byte[] RowVersion { get; set; }
}
Seeding:
var seed = new MyEntity{ Id = -2, StateChanges = new List<StateChange>(), OrderId = -2 };
modelBuilder.Entity<MyEntity>().HasData(seed);
I have been trying various things with the [Key] and [DatabaseGenerated] but could not find a solution. The version of all the ef core nuget dependencies is 3.1.5
Change the class
IEntityObject
to an interface.Here is a fully working console project that assumes that
StateChange
is some internal class of yours and not a model/entity class:In case
StateChange
is supposed to be a model class/entity as well, then you should add a foreign key to it and setup the relationship. This could look like this: