TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

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

[Bug] Transformers work incorrectly. #85

Closed MyEidos closed 5 years ago

MyEidos commented 5 years ago

Steps to replicate: Add transformers services.AddHandlebarsTransformers( navPropertyTransformer: e => new EntityPropertyInfo("prefix" +e.PropertyType, e.PropertyName), entityNameTransformer: n => "prefix" + n, entityFileNameTransformer: n => "prefix" + n);

Scaffold.

Observe issues in entity constructors: public Cat() { Paws = new HashSet<Paw>(); }

Note that new HashSet() is incorrect since entity should have "prefixPaw" type.

tonysneed commented 5 years ago

This is working as expected. To update the name of the Paw type, you need to specify the entityNameTransformer argument of AddHandlebarsTransformers. Have a look at how this is done in the ScaffoldingDesignTimeServices sample.

// Add optional Handlebars transformers
services.AddHandlebarsTransformers(
    entityNameTransformer: n => n + "Foo",
    entityFileNameTransformer: n => n + "Foo",
    constructorTransformer: e => new EntityPropertyInfo(e.PropertyType + "Foo", e.PropertyName + "Foo"),
    propertyTransformer: e => new EntityPropertyInfo(e.PropertyType, e.PropertyName + "Foo"),
    navPropertyTransformer: e => new EntityPropertyInfo(e.PropertyType + "Foo", e.PropertyName + "Foo"));

Instead of the "Foo" suffix, you would supply your "prefix" prefix.

MyEidos commented 5 years ago

Hi @tonysneed Thanks for answer. But I already had "entityNameTransformer". Please see my example. Do I miss something?

tonysneed commented 5 years ago

Ah yes, you need to supply the navPropertyTransformer argument.

MyEidos commented 5 years ago

Yep. It is an issue. I don't want to change "navPropertyTransformer". I mean "name" for navigation properties.

tonysneed commented 5 years ago

Why not? Let me try to replicate ...

tonysneed commented 5 years ago

I created a demo project which you can have a look at: https://github.com/tonysneed/HbsTransformersDemo

There I define a ScaffoldingDesignTimeServices class that contains the following code.

services.AddHandlebarsTransformers(
    entityNameTransformer: n => "Prefix" + n,
    entityFileNameTransformer: n => "Prefix" + n,
    constructorTransformer: e => new EntityPropertyInfo("Prefix" + e.PropertyType, e.PropertyName),
    navPropertyTransformer: e => new EntityPropertyInfo("Prefix" + e.PropertyType, e.PropertyName));

After running the dotnet ef dbcontext scaffold command against the NorthwindSlim database for the Category and Product tables, I get the following output for the Category table.

public partial class PrefixCategory
{
    public PrefixCategory()
    {
        Product = new HashSet<PrefixProduct>();
    }

    public int CategoryId { get; set; }
    public string CategoryName { get; set; }

    public virtual ICollection<PrefixProduct> Product { get; set; }
}

I believe the constructor represents what you're trying to achieve?

Note that the transformers work uniformly for all generated types, so the class is PrefixCategory and not simply Category. If you wish to exercise more control, then you should extend HbsCSharpEntityTypeGenerator and override GenerateProperties, as suggested in my answer to issue #84.

tonysneed commented 5 years ago

Closing this as invalid. @MyEidos please let me know if this is not the case.