Closed JustinianErdmier closed 10 months ago
With settings to auto generate EfCoreValueConverter, all i had to do to create an EF Core migration was to define HasConversion for property in entity specific type configuration with fluent api.
public class AddressEntityTypeConfiguration : IEntityTypeConfiguration<AddressEntity>
{
public void Configure(EntityTypeBuilder<AddressEntity> builder)
{
builder
.Property(t => t.Id)
.HasConversion<AddressId.EfCoreValueConverter>()
.ValueGeneratedOnAdd()
.IsRequired();
}
}
Thanks for the info on where you struggled, I'll update the documentation to try to make it clearer how to work with these libraries.
By the way, in .NET 6+, you can define a convention based on a type not just a property, so you can do something like this:
internal class ConventionsDbContext : DbContext
{
public ConventionsDbContext(DbContextOptions options) : base(options)
{
}
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder
.Properties<AddressId>()
.HaveConversion<AddressId.EfCoreValueConverter>(); // Always use the converter any time AddressId is used
}
}
I'm going to close this one for now as I think it's mostly resolved, but I'll aim to do a re-hash of the documentation, thanks!
TL;DR
Is the
StronglyTypedIdValueConverterSelector
you discuss in your article Strongly-typed IDs in EF Core (Revisited) a type that which should be created by the consumer?Context
The one thing I struggle with as I try to adopt your library is that your articles are heavily referenced in the repo's README as well as most of your answers to issues/questions, yet the articles are written with the perspective of authoring your library - not consuming it. For example, in your article, you discuss the how and why of creating the EF Core value converter, but when consuming the library, all that's done is setting a flag.
With that said, I'm having an issue creating my first migration. I only have one entity with which I am using a strongly typed id partially generated with your library. Including that entity, I have the following code:
Every time, the migration fails with the following message (I've shared it as an external resource simply because of the copious amount of text): Failed EF Core Migration Output with Strongly-typed Id. The main gist though is the following:
Attempts to Resolve
I have tried the following both individually and in combination with each other:
ApplicationUserProfileImage
entity like so:public ApplicationUserProfileImageId Id { get; set; }
.builder.Property(aupi => aupi.Id).HasConversion<Guid>();
.I've also source navigated to the file generated by your library,
ApplicationUserProfileImageId.g.cs
, and ensured the nestedEfCoreValueConverter
class was present.Then, I tried doing what your article says and attempted to replace the service when I add the DbContext to the dependency injection container:
As I thought,
StronglyTypedIdValueConverterSelector
is not a valid option; neither is qualifying it withStronglyTypedIds
namespace. After reading some of your answers to other issues/questions, I realize that your library is solely a source generator and does not provide any consumable code.Final Thoughts
I'm almost certain that the answer is "Yes, you have to create and register the
StronglyTypedIdValueConverterSelector
class yourself." Even so, I want to do my due diligence and ask and provide some explicit clarity for anyone else who might have the same confusion but is scared/shy to ask.After posting this, I am going to attempt to manually create and register the value converter selector class. I'll come back and edit this post with an update on my results.
Edit
It worked!
To those who might be thinking the same thing as me and/or looking for the answer to this very question,
There are some things to note. If you source navigate to the file generated by the library of any of your strongly typed ids, you'll see that the name of the nested class is different than that used in the article. This could technically change in the future, so it's best to check and confirm yourself, but at the time of writing this, the class name was
EfCoreValueConverter
. You'll want to ensure you fix it on around line 26 of the original snippet in the article, like so:I also updated the class to be stricter with nullable types and removed a lot of nesting. I haven't had a chance to actually test it (i.e., writing/reading data), but I was able to create my migration correctly.