TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

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

[Feature request] Have a way to know that a property is part of the object identifier (PK) #76

Closed omatrot closed 5 years ago

omatrot commented 5 years ago

During TypeScript entities generation, I need to know if a property is part of the object identifier.

I need to tell serializr by specifiying:

identifier(): Serialize a field as primitive value, use it as identifier when serializing references (see reference)

Each navigation property is already always a reference.

It seems that this information may be available during csharp entities generation.

I'll be happy to make a pull request for that feature. Any further guidance would be very welcome.

tonysneed commented 5 years ago

How can you determine the object identifier? Will property name and or type help you?

omatrot commented 5 years ago

The object identifier is the combination of the column(s) of the primary key. The type and/or name are not enough.

tonysneed commented 5 years ago

It is in this type of situation that the extensibility of the framework really shines. What you need to do is create a class that inheritance from HbsTypeScriptEntityTypeGenerator, Then add an is-object-identifier to the data that is surfaced to the handlebars template.

Let me know if you need some assistance.

omatrot commented 5 years ago

I see that I can create a class that inherits HbsTypeScriptEntityTypeGenerator but I can't see how it will be used instead of the base class. There must be some method to call in ConfigureDesignTimeServices ?

tonysneed commented 5 years ago

What you'll need to do is register MyHbsTypeScriptEntityTypeGenerator in ScaffoldingDesignTimeServices, after calling services.AddHandlebarsScaffolding, like so:

services.AddSingleton<ICSharpEntityTypeGenerator, MyHbsTypeScriptEntityTypeGenerator>();
omatrot commented 5 years ago

The code runs but now I have my properties with c# types instead of TypeScript types :|

EDIT: But I'm now able to detect primary key fields correctly :)

tonysneed commented 5 years ago

You need to use TypeScriptHelper. Copy code from HbsTypeScriptEntityTypeGenerator.