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:
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?