HasData seeding causes Add to fail with PostgresException: 23505 on calling SaveChanges
See original GitHub issueNpgsql.EntityFrameworkCore.PostgreSQL version: 2.2.0-preview1
HasData
seeding causes Add
to fail when calling SaveChangesAsync
.
Example:
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ItemConfiguration : IEntityTypeConfiguration<Item>
{
public void Configure(EntityTypeBuilder<ProductGroup> builder)
{
builder.HasData(new Item { Id = 1, Name = "Test" });
}
}
Now when you want to add new Item
, this code won’t work and will cause PostgresException: 23505
:
var item = new Item { Name = "Cool item" };
db.Add(item);
await db.SaveChangesAsync(); // PostgresException: 23505
Note that this code will work fine if we won’t use HasData
.
What you have to do instead, you have to set Id explicitly to 0:
var item = new Item { Id = 0, Name = "Cool item" }; // default or default(int) doesn't work, it must be 0...
Or it starts to work too after inserting the item manually using some DB manager.
I think https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/367 and https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/573 is related?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Avoid HasData Seed DbContext in EF Core 2.2 during Unit ...
I want to seed my unit tests with different, very specific data. Because EnsureCreated() seeds the context, and then I try adding my...
Read more >Data Points - Deep Dive into EF Core HasData Seeding
Now, when I try to add a migration to account for these new changes (the IsRequired method and seeding two more magazines), the...
Read more >Applying Seed Data To The Database
Applying seed data to a database refers to the process of inserting initial data into a database, usually when the database is first...
Read more >Sequelize Duplicate Key Value Violates Unique Constraint.
We seed a user ( John) firstly, then insert multiple users in bulk So there ... 5 PostgresException: 23505: duplicate key value violates...
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
-1 is more negative, we need more positivity in the world. Now seriously, -1 is not worse technically. But me having to manually write this in the migrations sucks, Especially during development because migrations will get deleted multiple times, then you forget to add 1 line there (or maybe someone else in the team doesn’t yet know about this problem). This will result in a lot of time wasted on debugging.
If you consider how many people wasted a few hours researching why this happens there are probably thousands of work hours lost because of this not working as intended.
Don’t use HasData: https://github.com/aspnet/EntityFramework.Docs/issues/1055