C
C#3w ago
dvdmeer

Data seeding with anonymous objects in .NET 9

I recently began migrating some projects from .NET 8 to .NET 9. One of the things I now run into is the Seeding part with Entity Framework Core. My seeding in my projects is done in the OnModelCreating and I add an extra environment variable to ensure that this part does not happen all the time. I then use something like:
modelBuilder.Entity<MyDtoClass>().HasData(
new
{
...
});
modelBuilder.Entity<MyDtoClass>().HasData(
new
{
...
});

The reason I am using anonymous objects is that I make use of shadow properties for several objects and I fill these properties in my override of SaveChangesAsync. This way I just just add a couple of marker classes to some of my DTO objects like ICreateable, IUpdateable or ISoftDeletable and then these shadow properties are automatically filled when required. For Seeding my data I generate certain information dynamically (e.g. GUIDs and dates/times) which has not been a problem in .NET 8 but I do turn off the warning I get:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.PendingModelChangesWarning));
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.PendingModelChangesWarning));
}
Now with .NET 9 there is a new way of seeding data. So, the data seeding happens somewhere else. The example Microsoft gives is at: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-9.0/whatsnew#improved-data-seeding
context.Set<Blog>().Add(new Blog { Url = "http://test.com/" });
context.SaveChanges();
context.Set<Blog>().Add(new Blog { Url = "http://test.com/" });
context.SaveChanges();
Unfortunately by using this context.Set<T>().Add method I am no longer able to add an anonymous object so it now becomes impossible for me to use with the shadow properties as they are not defined for my DTOs. How can I handle this scenario without going back to the old way?
What's New in EF Core 9
Overview of new features in EF Core 9
9 Replies
Jimmacle
Jimmacle3w ago
what do you need access to the shadow properties for? anonymous objects sounds cursed, if you need access to the properties in your code then just make them regular properties that is also not a new way of seeding data, that's just using EF Core to access your database - the article is pointing out the UseSeeding method specifically as a new convenience but you could always just write your own seeding code (also, that article suggests it only works with EnsureCreated which you probably shouldn't be using)
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
jcotton42
jcotton422w ago
@TeBeCo @Jimmacle if you click through to the full article
The new seeding methods are called as part of EnsureCreated operation, Migrate and dotnet ef database update command, even if there are no model changes and no migrations were applied.
jcotton42
jcotton422w ago
Data Seeding - EF Core
Using data seeding to populate a database with an initial set of data using Entity Framework Core
Jimmacle
Jimmacle2w ago
either way, it doesn't really affect the core of the problem frankly i didn't even know you could just throw anonymous objects at EF and it seems like a bad idea i do my seeding the "new" way but without the literal UseSeeding builder method, it's just in my app startup
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle2w ago
because the "new way" code quoted by op is not really what the article is pointing out as the new way, it's just normal ef code
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2w ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?