Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extension method for EF core #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

idan-h
Copy link

@idan-h idan-h commented Jun 19, 2022

#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

Extension method which dynamically registers the id conversion, and if needed automatically uses Identity
@andrewlock
Copy link
Owner

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?

@idan-h
Copy link
Author

idan-h commented Jul 11, 2022

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

@andrewlock
Copy link
Owner

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

@idan-h
Copy link
Author

idan-h commented Jul 13, 2022

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

@Rudde
Copy link

Rudde commented Sep 1, 2022

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.

@benlongo
Copy link

benlongo commented Sep 8, 2022

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.

@jeffward01
Copy link

jeffward01 commented Jan 8, 2023

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants