Seeding data: The seed entity for entity type 'X' cannot be added because there was no value provided for the required property 'Id'.
See original GitHub issueI’m using EntityFrameworkCore 2.1.0-preview2-final
I try to seed data, this works:
modelBuilder.Entity<Dog>().HasData(new Dog[]
{
new Dog() { Id = 1, Name = "Lucy", Age = 2 },
new Dog() { Id = 2, Name = "Bella", Age = 3 },
new Dog() { Id = 3, Name = "Daisy", Age = 5 },
});
this doesn’t:
modelBuilder.Entity<Dog>().HasData(new Dog[]
{
new Dog() { Name = "Lucy", Age = 2 },
new Dog() { Name = "Bella", Age = 3 },
new Dog() { Name = "Daisy", Age = 5 },
});
And I get:
The seed entity for entity type ‘X’ cannot be added because there was no value provided for the required property ‘Id’.
public class BaseEntity
{
public int Id { get; set; }
}
public class Dog : BaseEntity
{
public string Name { get; set; }
public int Age { get; set; }
}
I expected it to work even when skipping Id. Is it auto increment by default?
I wanted to get it working with Bogus:
var dogsIds = 0;
var testDogs = new Faker<Dog>().RuleFor(p => p.Name, f => f.Name.FirstName())
.RuleFor(p => p.Age, f => DateTime.Now.Year - f.Person.DateOfBirth.Year)
.RuleFor(p => p.Id, f => dogsIds++);
var dogs = testDogs.Generate(100);
modelBuilder.Entity<Dog>().HasData(dogs.ToArray());
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:14 (6 by maintainers)
Top Results From Across the Web
The seed entity for entity type 'X' cannot be added because ...
I get "The seed entity for entity type 'Role' cannot be added because a non-zero value is required for property 'Id'.
Read more >The seed entity for entity type 'Product' cannot be ... - YouTube
The seed entity for entity type 'Product' cannot be added because another seed entity with the same key value for {' Id '}...
Read more >The seed entity for entity type 'X' cannot be added because a ...
Problem. "The seed entity for entity type 'Skill' cannot be added because a non-zero value is required for property 'Id.' Consider providing ...
Read more >C# – The seed entity for entity type 'X' cannot be added ...
C# – The seed entity for entity type 'X' cannot be added because the was no value provided for the required property “..ID”....
Read more >'The seed entity for entity type 'MyEntity' cannot be added ...
I got this error recently while playing with EF Core 2. There's very little on Google about it; although it's not a hugely...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Can the error message
be changed to
I just wasted a whole day trying to figure out why it was not working only to discover that I needed to start incrementing from 1 instead of 0.
Bruh…Same here.
What’s the point of ModelBuilder if I should manually add autogenerated properties?! End up writing custom Seeder like I would create it with EF
Then what’s the point in ModelBuilder.HasData() in the first place ?!