question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HasData seeding causes Add to fail with PostgresException: 23505 on calling SaveChanges

See original GitHub issue

Npgsql.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:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
BojidarStanchevcommented, Jan 17, 2019

-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.

0reactions
bugproofcommented, Jan 18, 2019
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found