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:
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: 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 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?
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: 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 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?
18 replies
❔ Add content negotiation to my .net 7 Minimal API project
.NET 7 Minimal API does not do content negotiation and as such always returns JSON.
What if I would like my Minimal API to return XML, based on the Accept request header?
So far I can create an endpoint filter and have some basic code that checks for the Accept header that I want but a few things are not clear to me:
1. How can I get and manipulate the response body?
2. I noticed that for my API, when I send an authentication request "result" will be of type Ok<AuthResult> where "AuthResult" is one of my model classes that holds my token and some other info. As this is not yet JSON I assume that the conversion to JSON happens at a later stage. Is this correct?
3. If it happens at a later stage, how can I then manipulate the body and send back the response in XML instead?
4. I assume that when I have it working for my single authentication endpoint in order to make it work for the rest I will need to create an endpoint filter factory in order to make it generic enough so that it will work for other objects also?
Note that my sample filter does not do anything yet as I have yet to figure out how to add the rest.
public class SampleEndpointFilter : IEndpointFilter
{
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
var result = await next(context);
if (context.HttpContext.Request.Headers.Accept.ToString().ToUpper() == "APPLICATION/XML")
{
}
return result;
}
}
Can anyone help me adding the necessary code to let me change the JSON body to XML instead? Or at least help me on my way?
Thanks
5 replies