-
Notifications
You must be signed in to change notification settings - Fork 54
Description
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
- 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); });
- Create a database with three tables:
- Table
Userwith columnUserIdas primary key - Table
Subscriptionwith columnSubscriptionIdas primary key - Table
UserSubscriptionwith columnsUserIdandSubscriptionIdas combined primary key - Add foreign keys to table
UserSubscription
- Table
- Scaffold from the database with handlebars.
- 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.