Skip to content

[Bug] GenerateManyToMany produces wrong code when using AddHandlebarsTransformers #216

@Herdo

Description

@Herdo

Describe the bug
HbsCSharpDbContextGenerator.GenerateManyToMany does not respect configured design time service transformations (Microsoft.EntityFrameworkCore.Design.IDesignTimeServices.ConfigureDesignTimeServices). When AddHandlebarsScaffolding is configured with propertyTransformer, wrong IndexProperty<T> values are configured, basically ignoring AddHandlebarsScaffolding.

As example, the generated index property may look like this: j.IndexerProperty<int>("UserId").HasColumnName("UserId");

This wrong (or rather missing) transformation is causing a runtime issue:

The relationship from 'UserSubscription (Dictionary<string, object>)' to 'User' with foreign key properties {'UserId' : int} cannot target the primary key {'UserId' : InternalUserId} because it is not compatible. Configure a principal key or a set of foreign key properties with compatible types for this relationship.

Expected behavior
The configured EntityPropertyInfo of the propertyTransformer in AddHandlebarsTransformers would also transform the type of the generated IndexProperty<T>.
For example, for properties matching the name UserId map to the type MyNamespace.InternalUserId, the index property should be generated as IndexProperty<MyNamespace.InternalUserId>.

To Reproduce

  1. Implement a IDesignTimeServices:
    serviceCollection.AddHandlebarsTransformers(propertyTransformer: p =>
    {
        // Map to strong types
        if (p.PropertyName.Contains("UserId"))
            return new EntityPropertyInfo("MyNamespace.InternalUserId", p.PropertyName);
        // Use default
        return new EntityPropertyInfo(p.PropertyType, p.PropertyName);
    });
  2. Create a database with three tables:
    • Table User with column UserId as primary key
    • Table Subscription with column SubscriptionId as primary key
    • Table UserSubscription with columns UserId and SubscriptionId as combined primary key
    • Add foreign keys to table UserSubscription
  3. Scaffold from the database with handlebars.
  4. Execute any logic using the scaffolded DbContext.

Make My Life Easier
I'll add this:
If possible, create a pull request that contains a failing unit test.
Otherwise, use the sample/ScaffoldingSample project to reproduce the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions