When I add a migration after filling out some HasData() on a modelBuilder.Entity<T>(), the migration works well. However, if I add another migration immediately following (without changing anything), the second migration deletes and re-inserts all of the seed data again.
I asked a SO question yesterday, because I thought this was occurring because I was trying to seed from an external data source (.json files). However, I am seeing the same issue today with inline new-ing of seed data as the docs demonstrate.
Any given piece of seed data, if left unmodified in HasData(), should only appear in Up() of a single migration, correct? This is my first go at using EF Core migrations with seed data, so it is certainly possible that I have something configured incorrectly, or I missed something in how the feature is supposed to work.
This could very well be a Pomelo (provider) issue as well. I am not versed enough in EF Core to know how to discern which it might be.
MyDbContext.Database.Migrate();. Verified that seed data was successfully added to the db.uint -> int for IdsSince I am somewhat swimming upstream using uints as Id types (blocked by https://github.com/aspnet/EntityFrameworkCore/issues/11597 / https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/601), I tried temporarily converting all of these to ints, with the same results.
DateTime/TimestampBecause I do have a couple DateTimes stored as Timestamps in this entity, I tried a variant removing the DateTime.Now calculation of these values thinking that the dynamic nature of these properties caused a re-generation of the seed data in the migration. (ref: https://github.com/aspnet/EntityFrameworkCore/issues/11969)
DateTime.Now().DateTime.Now().EF Core version: 2.1.1
Database Provider: Pomelo.EntityFrameworkCore.MySql (2.1.1)
Operating system: Windows 10
IDE: Visual Studio 2017 15.8.2
@collinbarrett Did you try using int Ids __and__ removing dynamic temporal values at the same time?
@AndriySvyryd Good suggestion. Should've thought to try that. Will try that and provide any updates.
@AndriySvyryd added "Alternate 3" to the OP. No dice.
Also experiencing this exact same issue, will endeavour to remove the duplicated InsertData and DeleteData statements from generated migrations until 2.2.0 drops.
@chrispickford , which EF Provider are you using? Curious if it might be provider-specific or EF Core in general.
@collinbarrett Here's my context factory, I'm using SqlServer:
public class MyContextFactory : IDesignTimeDbContextFactory<MyContext>
{
public MyContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
optionsBuilder.UseSqlServer(@"Server=.\SQLEXPRESS;Database=Test;Trusted_Connection=True;");
return new MyContext(optionsBuilder.Options);
}
}
In MyContext.cs, nothing fancy going on:
modelBuilder.Entity<Brand>(b =>
{
b.HasData(SeedData.BrandData);
});
Ok, I'm using Pomelo for MySQL, which is a bit off the main trail of SQL Server. So, I was thinking it could be a bug with that provider. So, thanks for verifying it occurs with SQL Server as well.
Is SeedData.BrandData just hard-coded new-ed data similar to what I was doing here?
Yes, just another static class for seed data, exactly like yours in fact.
I thought it might be due to using dynamic values, i.e. Guid.NewGuid(), I've since changed to using static hard-coded values and the issue still occurs.
internal static readonly Brand[] BrandData =
{
new Brand {Id = 1, Name = "Brand X", Uuid = new Guid("e5f9762f-44ce-456e-b226-c8b2ae733f72")}
};
Yeah, i have this problem too. The migrations are created just fine when the primary key is an int, but with the entities that have a string as primary key the migrations delete and re-insert the data instead of updating it.
"Alternate 2" now works correctly with the latest nightly build. Probably fixed by https://github.com/aspnet/EntityFrameworkCore/commit/c8e49968c7aa60a8e0f32da3ecedad5e7ffa3b59
Thanks! Will test that out.
Issue is related to https://github.com/aspnet/EntityFrameworkCore/issues/13172