Closed gpender closed 2 years ago
We are able to make these mappings changes using the AddHandlebarsTransformers method calls but the fluent code for the context does not correctly add the .ToTable and does not ever add the .AsColumnName.
@gpender Are you saying there is a problem with generated code for the the context OnModelCreating
method? If so, could you please provide an example?
Hi @tonysneed , I don't see any problems with the generated code unless I need to specify a different entity name for a table and therefore need the .ToTable fluent call, and/or, I need to specify a different property name for a column and therefore need the .HasColumnName fluent call. Does that make sense? It was a few weeks ago and I have context switched since then :) Happy to provide more info if you need me to. Thanks for all your efforts
The most recent release includes fixes to generating OnModelCreating. Could you please try it again to see if the same behavior is exhibited? If so, could you please provide an example of the problem you are encountering?
Hi @tonysneed I believe I have the latest version and my request is still valid.
So for the Entities with different table names I need the generated code to look like this:-
modelBuilder.Entity<ConfiguredPricePlan>(entity =>
{
entity.ToTable("PricePlan");
I added a transformation service for entity type name but this does not seem to be used in the logic for adding the ToTable fluent method. The code I wrote to make this happen is in my original post
For the Columns with different property names I need the generated code to look like this:-
entity.Property(e => e.Tail)
.HasColumnName("TailPrefix")
When I search through the code base there is no code that implements HasColumnName.
The other part of my request was the DI of the IContextTransformationService so that the context can be made available when
using the other transforms:-
entityNameTransformer: MapEntityName,
constructorTransformer: MapPropertyInfo,
propertyTransformer: MapPropertyInfo,
navPropertyTransformer: MapNavPropertyInfo,
entityFileNameTransformer: MapEntityNamethe
I hope this adds more clarity to my request and thanks again
@gpender Thanks for getting back to me with additional info on your enhancement request.
It sounds like there could be two separate PR's:
IContextTransformationService
available to transformersLet's work on the first PR before considering the second. Would you be able to create a PR for the first with a couple of unit tests -- one for table name and another for column name? You might want to have a look at the the Contributing Guidelines for how to get started. Thank you in advance!
@tonysneed have made my code changes for part 1/2 and added a couple of unit tests and created a pull request. Hope its ok :)
@tonysneed I am now looking at the second part of my request and wanted to clarify why I wanted it. When I was writing the design time transformer class I created a JSON lookup file for my mappings:-
"Name": "EntitiesOne:Aircraft",
"NameOverride": "MyAircraft",
"Mappings": [
{
"Name": "Livery",
"NameMapping": "MyLivery"
}
]
To correctly select the mappings:-
So I need context and so I need the TransformContextFileName method to be called early on in my Transformer class so I can store the context locally. Currently it is only called when the model is generated in the HbsCSharpModelGenerator. Hope this makes sense.
@tonysneed part 2/2 done and existing unit tests all still passing
@gpender Created #198 for part 2 of this issue.
Problems We are using this library in an ef core database first application and we have some name changes between our database tables and entity names and our column names and entity properties.
We are able to make these mappings changes using the AddHandlebarsTransformers method calls but the fluent code for the context does not correctly add the .ToTable and does not ever add the .AsColumnName.
In addition to this it would be very useful to know the context name when making the transforms against the following methods entityNameTransformer: MapEntityName, constructorTransformer: MapPropertyInfo, propertyTransformer: MapPropertyInfo, navPropertyTransformer: MapNavPropertyInfo, entityFileNameTransformer: MapEntityName
Workarounds We have worked around both of these issues by implementing our own MyHbsCSharpDbContextGenerator with the following code changes:-
Add the IContextTransformationService contextTransformationService to the constructor then execute in the WriteCode override. The contextFileNameTransformer can then store the context locally for use in the other transformers.
In the GenerateProperty method check for a property name transformation and add the .HasColumnName as necessary.
In the GenerateTableName method check for a table name transformation in addition to explicitSchema checks