TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

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

NullReferenceException with EntityTypeTransformationService.TransformNavPropertyName #136

Closed gdycus closed 3 years ago

gdycus commented 3 years ago

Getting the following error when executing "dotnet ef dbcontext scaffold". Any thoughts on what might cause this error? Thanks!

System.NullReferenceException: Object reference not set to an instance of an object. at EntityFrameworkCore.Scaffolding.Handlebars.HbsCSharpDbContextGenerator.GenerateRelationship(IForeignKey foreignKey, Boolean useDataAnnotations, IndentedStringBuilder sb) at EntityFrameworkCore.Scaffolding.Handlebars.HbsCSharpDbContextGenerator.GenerateEntityType(IEntityType entityType, Boolean useDataAnnotations, IndentedStringBuilder sb) at EntityFrameworkCore.Scaffolding.Handlebars.HbsCSharpDbContextGenerator.GenerateOnModelCreating(IModel model, Boolean useDataAnnotations) at EntityFrameworkCore.Scaffolding.Handlebars.HbsCSharpDbContextGenerator.GenerateClass(IModel model, String contextName, String connectionString, Boolean useDataAnnotations, Boolean suppressConnectionStringWarning) at EntityFrameworkCore.Scaffolding.Handlebars.HbsCSharpDbContextGenerator.WriteCode(IModel model, String contextName, String connectionString, String contextNamespace, String modelNamespace, Boolean useDataAnnotations, Boolean suppressConnectionStringWarning) at EntityFrameworkCore.Scaffolding.Handlebars.HbsCSharpModelGenerator.GenerateModel(IModel model, ModelCodeGenerationOptions options) at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions) at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable1 schemas, IEnumerable1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable1 schemaFilters, IEnumerable1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>cDisplayClass0_0.<.ctor>b0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>cDisplayClass3_0`1.b0()

gdycus commented 3 years ago

I'm using 3.1.7 versions of EntityFrameworkCore components

ErikEJ commented 3 years ago

Which version of the Handlebars package?

Which EF Core provider and version?

gdycus commented 3 years ago

Microsoft.EntityFrameworkCore = 3.1.7 Microsoft.EntityFrameworkCore.Design = 3.1.7 Microsoft.EntityFrameworkCore.SqlServer = 3.1.7 Microsoft.EntityFrameworkCore.Tools = 3.1.7 EntityFrameworkCore.Scaffolding.Handlebars = 3.8.2

ErikEJ commented 3 years ago

Possible related to https://github.com/ErikEJ/EFCorePowerTools/issues/463

ErikEJ commented 3 years ago

We need your database schema

gdycus commented 3 years ago

I just emailed it to you @ErikEJ

gdycus commented 3 years ago

@tonysneed could I send the schema to your private email? If so, could you send your email address?

gdycus commented 3 years ago

Or you can email me at grady at singenuity com and I'll reply with the schema

FYI I do not have EF Power Tools installed

tonysneed commented 3 years ago

@gdycus If you run the command dotnet ef dbcontext scaffold, without involving Handlebars at all, you will get an error. So this is not a bug in Handlebars templates, but seems to be related to your model:

The column 'Merchandise.Products.Taxable' would normally be mapped to a non-nullable bool property, but it has a default constraint. Such a column is mapped to a nullable bool property to allow a difference between setting the property to false and invoking the default constraint. See https://go.microsoft.com/fwlink/?linkid=851278 for details.

ErikEJ commented 3 years ago

Why would that warning cause a NRE?

tonysneed commented 3 years ago

@ErikEJ You're right. The NRE takes place simply because I need to use the null propagation operator. @gdycus I'll release a fix ASAP.

tonysneed commented 3 years ago

@ErikEJ @gdycus Fix has been published with release v3.8.3 on NuGet.

ErikEJ commented 3 years ago

Perfect, I just updated EF Core Power Tools, and the fix is available in the latest daily build

gdycus commented 3 years ago

It's working now. Thanks!