StronglyTypedId icon indicating copy to clipboard operation
StronglyTypedId copied to clipboard

Added extension method for EF core

Open idan-h opened this issue 2 years ago • 7 comments

#59

Extension method which dynamically registers the id conversion, and if needed automatically uses Identity

Use inside the DbContext OnModelCreating:

foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
    if (ReflectionHelper.IsAssignableToGenericType(entityType.ClrType, typeof(BaseEntity<>)))
    {
        modelBuilder.RegisterStrongTypedIdDynamically(entityType, this);
    }
}

Tested only with postgresql, tests with different dbs might be needed

It uses

.HasConversion<XXX.EfCoreValueConverter>()

And

.UseIdentityColumn()

If needed

idan-h avatar Jun 19 '22 21:06 idan-h

Hi @idan-h, thanks for this, I think people will find it very useful!

Unfortunately, I don't think this is a great fit for the library itself - the StronglyTypedId library is a source generator only, so by default you don't want the dll copied to the build output, which means these methods aren't available 🤷

I think this is maybe better placed as a blog post/documentation in the README?

andrewlock avatar Jul 11 '22 20:07 andrewlock

Yea, this is why I added it in a seperate project, so it could be shipped in a different nuget package.

If you want to add it to documentation thats fine by me, but I think in the long term there would be many more helper methods like this, so the real life integration would be feasible.

You might want to consider adding another repo, or combine that in the current repo and ship different nuget package

idan-h avatar Jul 11 '22 20:07 idan-h

Yea, this is why I added it in a seperate project, so it could be shipped in a different nuget package.

🤦‍♂️ My bad, I'm clearly tired, I read that as being src/StronglyTypedIds/Efcore. That makes much sense.

I think you're probably right, as a separate package of "helpers" does make sense 🙂 My concern here is the heavy use of reflection to do this, which feels quite fragile. I'll have a little think about it and see

andrewlock avatar Jul 11 '22 21:07 andrewlock

Yea, that's true, but I think there is no way out of reflection on this one.. Its either you write a lot of boilerplate code or use reflection Tests are sure needed

idan-h avatar Jul 13 '22 09:07 idan-h

This seems very interesting. Could this be done as it's own nuget if it won't be merged in? Would also be useful to update the README.md when new functionality like this is introduced.

Rudde avatar Sep 01 '22 20:09 Rudde

Would it be possible have a source generator output a method with signature void ConfigureConventions( ModelConfigurationBuilder configurationBuilder ) that accumulates all of the known StronglyTypedId objects with EfCoreValueConverters and registers all of them? I could see it being tricky to opt into/out of that, but the heuristic of just having a ef core converter at all seems like it should suffice for most cases.

benlongo avatar Sep 08 '22 16:09 benlongo

It looks like this method adds a configuration for ALL entities, ONLY on the Id property.

For example, I have a class like

public class SomeClass
{
    public Guid Id { get; set; }

    // this is a FK
    public MyRelatedEntityClassId MyRelatedEntityClassId { get; set; }

    public virtual MyRelatedEntityClass MyRelatedEntityClass { get; set; }
}

public class MyRelatedEntityClass
{
    // this has 'many' of the 'someCLass'
    public virtual ICollection<SomeClass> SomeClasses { get; set; }
}


if i register your class like:


    builder.RegisterStrongTypedIdDynamically(entityType, this, "MyStrongId ");

I will get the error:

Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: The property 'MyStrongId ' cannot be added to the type 'MyRelatedEntityClass' because no property type was specified and there is no corresponding CLR property or field. To add a shadow state property, the property type must be specified.

I'll make some modifications in the morning and share if i get it working. Or, i'll just manually register in the configs...

jeffward01 avatar Jan 08 '23 12:01 jeffward01