lib4dev icon indicating copy to clipboard operation
lib4dev copied to clipboard

Unable rename foreign keys inside in OnModelCreating

Open alexbenitez opened this issue 5 years ago • 0 comments

Following the usage instructions here, I would like to map the table "Customer" to "Client" (so in C# I use Client, ClientId, etc... while in the database still is Customer, CustermerId, etc...)

I added the following transformation

services.AddHandlebarsTransformers(
                entityNameTransformer: n => n.Replace("Customer","Client"),
                entityFileNameTransformer: n => n.Replace("Customer", "Client"),
                constructorTransformer: e => new EntityPropertyInfo(e.PropertyType.Replace("Customer", "Client"), e.PropertyName.Replace("Customer", "Client")),
                propertyTransformer: e => new EntityPropertyInfo(e.PropertyType.Replace("Customer", "Client"), e.PropertyName.Replace("Customer", "Client")),
                navPropertyTransformer: e => new EntityPropertyInfo(e.PropertyType.Replace("Customer", "Client"), e.PropertyName.Replace("Customer", "Client"))
                );

The generated Entities (in the Models folder) are properly generated as expected (e.g. Client.cs, ClientSettings.cs, ClientId, etc) however the NorthwindSlimContext.cs file still has references to the old naming and therefore the project does not compile

entity.HasOne(d => d.Customer) // <== this should be Client!!
                    .WithMany(p => p.Order)
                    .HasForeignKey(d => d.CustomerId) // <== this should be ClientId!!
                    .HasConstraintName("FK_Orders_Customers");

Is it possible to customize the generated OnModelCreating? Or should I run PowerShell script to search and replace the generated DbContext to correct the code?

alexbenitez avatar Oct 31 '19 17:10 alexbenitez