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.

Add migration update all seeded data

See original GitHub issue

First of all, I had to put on stand-by ef core 3 upgrade due to this issue https://github.com/aspnet/EntityFrameworkCore/issues/18297 duplicate of https://github.com/aspnet/EntityFrameworkCore/issues/17851 released on 3.1-preview2

Every time I add a migration, all seeded data are updated, so thousand line of code for each migration. Here it is an example migrationBuilder.UpdateData( table: "Countries", keyColumn: "Id", keyValue: new Guid("5c60f693-bef5-e011-a587-80ee7300c695"), columns: new[] { "CurrencyId", "DefaultLanguageId" }, values: new object[] { new Guid("7d76825e-4470-4b45-a5b9-bd96267685c0"), new Guid("deb5f6a8-2e81-47c3-bd10-a89c43919dd7") });

This has happen to me in the past only when i use DateTime.Now in seed because, obviously, the value changes every time I add a migration, but not with GUID

Further technical details

EF Core version: Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: 3.1.100-preview3-014645 Operating system: latest stable VS2019, W10

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
AndriySvyrydcommented, Jul 22, 2020

Fixed in a8f0f4c4dfc94975bd318a6b5a5d3a627df1d5ba

2reactions
emre1702commented, Jan 29, 2020

I got a similar problem: Since Microsoft.EntityFrameworkCore 3.1.0 the command “Add-Migration” updates every seeded row having a colum with a default value and using this column in the seed method.

Example:

public class ChallengeSettings
{
    public int Id { get; set; }
    public int MinNumber { get; set; }
}

modelBuilder.Entity<ChallengeSettings>(entity =>
{
    entity.HasKey(e => e.Id);

    entity.Property(e => e.MinNumber)
        .HasDefaultValue(1);
});

modelBuilder.Entity<ChallengeSettings>().HasData(
    new ChallengeSettings { Id = -1, MinNumber = 50 },
    new ChallengeSettings { Id = -2, MinNumber = 1 },
    new ChallengeSettings { Id = -3, MinNumber = 5 }
);

The migration:

public partial class Test : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.UpdateData(
            table: "ChallengeSettings",
            keyColumn: "Id",
            keyValue: -3,
            column: "MinNumber",
            value: 5);

        migrationBuilder.UpdateData(
            table: "ChallengeSettings",
            keyColumn: "Id",
            keyValue: -2,
            column: "MinNumber",
            value: 1);

        migrationBuilder.UpdateData(
            table: "ChallengeSettings",
            keyColumn: "Id",
            keyValue: -1,
            column: "MinNumber",
            value: 50);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.UpdateData(
            table: "ChallengeSettings",
            keyColumn: "Id",
            keyValue: -3,
            column: "MinNumber",
            value: 5);

        migrationBuilder.UpdateData(
            table: "ChallengeSettings",
            keyColumn: "Id",
            keyValue: -2,
            column: "MinNumber",
            value: 1);

        migrationBuilder.UpdateData(
            table: "ChallengeSettings",
            keyColumn: "Id",
            keyValue: -1,
            column: "MinNumber",
            value: 50);
    }
}

The code of my program is much bigger so it can be pretty hard to always remove these “wrong” migrations (cause I wanted the migration to happen only at the beginning, so I can update these seeded values later without them getting overrided on every migration).

Problem occured with: EFCore 3.1.0 and 3.1.1. Npgsql.EntityFrameworkCore.PostgreSQL 3.1.0

Worked with: EFCore 3.0.2 Npgsql.EntityFrameworkCore.PostgreSQL 3.0.1

It could be Npgsql, too - can’t test it because Npgsql depends on EFCores specific versions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrations and Seed Data With Entity Framework Core
Using migrations is a standard way to create and update a database with Entity Framework Core. The migration process has two steps: Creating ......
Read more >
Data Seeding - EF Core
Unlike in EF6, in EF Core, seeding data can be associated with an entity type as part of the model configuration. Then EF...
Read more >
Applying Seed Data To The Database
You can seed related data in the database by creating multiple entities in the OnModelCreating method and defining relationships between them.
Read more >
EFCore 3 generate update for seed data in every migrations
After updating my project from dotnet core 2.2 to 3.1. Now, after running migrations add command, new migration contains update for all seed...
Read more >
every time use Add migration, ef wants to update all ...
Hi, I`m using separate files for seeding data for example i have file as BaseTablesSeedData.cs public class BaseTablesSeedData { public ...
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