5

Update 8/29/18

Seeing this issue in inline new-ing of seed data as well. Opened an EF Core issue. Will update the question with any findings.


I am trying to use EF Core 2.1's seeding mechanism. However, I want to load the seed data from json flat files rather than hard-coding new C# objects. I wrote the extension method below, which worked well for the initial migration. It looks for files named [MyEntity].json in DataPath and deserializes them into objects.

The problem is that if I add a subsequent migration, even without changing a single thing in the models, configurations, or json files, the new migration deletes every single entity and re-inserts them again. So, the Up() contains a bunch of deletes followed by a bunch of inserts of the same data. I suspect this is because EF Core is not smart enough to recognize that it is an identical set of seed data.

Question:

Is there a way to use EF Core 2.1 seeding with an external data source (such as json files) without having each migration delete and re-insert the data?

My seeding extension method:

public static class ModelBuilderExtensions
{
    public static string DataPath { private get; set; } = "..\\..\\data";

    public static void Seed<TEntity>(this ModelBuilder modelBuilder) where TEntity : class, IBaseEntity
    {
        var entities = GetSeedRows<TEntity>();
        modelBuilder.Entity<TEntity>().HasData(entities);
    }

    private static TEntity[] GetSeedRows<TEntity>() where TEntity : IBaseEntity
    {
        try
        {
            return JsonConvert.DeserializeObject<TEntity[]>(
                File.ReadAllText(DataPath + Path.DirectorySeparatorChar + typeof(TEntity).Name + ".json"));
        }
        catch (FileNotFoundException e)
        {
            Console.WriteLine(e.Message);
            return null;
        }
    }
}
Collin Barrett
  • 2,441
  • 5
  • 32
  • 53
  • I think you are searching for AddOrUpdate method from the classic EF. Check this https://stackoverflow.com/questions/36208580/what-happened-to-addorupdate-in-ef-7-core – GoldenAge Aug 28 '18 at 22:47
  • Thanks, @GoldenAge, but I was actually hoping to use the native EF Core 2.1 `HasData()` seeding. I'm not sure `AddOrUpdate()` would assist with that. That would be if I was going to roll my own seeding, I think? – Collin Barrett Aug 28 '18 at 23:05

1 Answers1

0

This issue was an EF Core bug that was resolved in EF Core 2.2.

Collin Barrett
  • 2,441
  • 5
  • 32
  • 53
  • 2
    I am using EF Core 2.2, but still, have this issue. Any pointers? – Priyan Perera Aug 10 '19 at 01:51
  • @PriyanPerera Late, but I had the same issue with `Guid` as my PK on EF Core 3.1, then I realized that each time I run seed, `new Guids` are generated. It is logical that EF will treat them as new records. The solution was to use static Guid values like `new Guid("8912aa35-1433-48fe-ae72-de2aaa38e37e")` for seed records PK. check this:https://learn.microsoft.com/en-us/archive/msdn-magazine/2018/august/data-points-deep-dive-into-ef-core-hasdata-seeding – Efe Dec 15 '20 at 20:47