TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

Scaffold EF Core models using Handlebars templates.
MIT License
208 stars 53 forks source link

property-type #138

Closed gdycus closed 3 years ago

gdycus commented 3 years ago

In the original .NET Entity Framework, a foreign key property name was always the name of the table. For example, if the foreign key was ClientLocationKey, the foreign key property generated would be ClientLocation. For some reason EF Core decided to name the property ClientLocationKeyNavigation. I'm migrating a very large legacy .NET EF project and I need to keep the same naming convention. I modified DbSets.hsb and Properties.hsb to use the property type instead of the property name and it worked, but the context is still generating references with the names containing the word "Navigation". Any thoughts on how to fix this references?

entity.HasOne(d => d.ClientLocationKeyNavigation) .WithMany(p => p.Orders) .HasForeignKey(d => d.ClientLocationKey) .OnDelete(DeleteBehavior.ClientSetNull) .HasConstraintName("FK_Orders_ClientLocations");

tonysneed commented 3 years ago

Your best bet I think would be to use a transformer. There is a navPropertyTransformer parameter you can set. This will propagate to the OnModelCreating method. If you use this approach, then you won't modify DbSets.hbs.

gdycus commented 3 years ago

The following "AddHandlebarsTransformers" is not having any affect on the property names. ReverseEngineerOptions and EnablePluralization works but not AddHandlebarsTransformers. Is there anything I need to do in addition to this?

services.AddHandlebarsScaffolding(options => { options.ReverseEngineerOptions = ReverseEngineerOptions.DbContextAndEntities;

options.EnablePluralization = true;

services.AddHandlebarsTransformers(
    navPropertyTransformer: e => new EntityPropertyInfo(e.PropertyType + "Foo", e.PropertyName + "Foo"));

});

gdycus commented 3 years ago

Disregard. It's working. I was calling AddHandlebarsTransformers inside of AddHandlebarsScaffolding. Thanks!