TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

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

Removing Keyless Attribute #156

Closed jryutzy closed 3 years ago

jryutzy commented 3 years ago

We have external tables in our Azure SQL DB that are scaffolded in with the DB Context. We can manually create relationships to them using the approach I outlined here to generate relationships so .Include() works to include these External tables and create a join in the LINQ query based on our manually created relationship. https://github.com/dotnet/efcore/issues/24373 Looks like it's been filed as a bug by EF Core team so I'm hoping Handlebars will let me remove it until they allow the Fluent API to overwrite the Data Annotation.

However, this requires us removing the [Keyless] attribute manually on the generated class. I'd like to use Handlebars to remove this attribute from classes that are in the Ext schema we use for External tables.

How can I prevent this attribute from being applied conditionally.

I can remove it from the context in the Handlebars Helper but it still gets generated on the .cs file for the entity. image

jryutzy commented 3 years ago

Is there somehow to add helpers like this called isdefined? https://handlebarsjs.com/guide/builtin-helpers.html#if image

I would like to check fi the value of class-annotation is [Keyless] and schema ends with .Ext then don't render the [Keyless] attribute

image

tonysneed commented 3 years ago

Hi @jryutzy, Thanks for your patience. I noticed that the EF Core team has created https://github.com/dotnet/efcore/issues/24369 to address your issue. They also suggested you try setting entity.Metadata.IsKeyless = false.

If you would still like to use Handlebars Scaffolding as a workaround, what you need to do is extend HbsCSharpEntityTypeGenerator and override GenerateEntityTypeDataAnnotations to omit generation of the Keyless attribute. This is an advanced technique, but you can find instruction here.

Let me know if you decide to take this approach and need any assistance.

tonysneed commented 3 years ago

Please reopen if there are further questions or concerns.