EFCorePowerTools icon indicating copy to clipboard operation
EFCorePowerTools copied to clipboard

EF Core 7 Reverse Engineer Error

Open aespitia opened this issue 2 years ago • 15 comments

Error when reverse engineering a model from an existing DB:

Microsoft SQL Server 2014 12.0.6439.10 (X64)

using EFCore 7 RC1

System.InvalidOperationException: Reverse engineer error: System.NullReferenceException: Object reference not set to an instance of an object. at string Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpDbContextGenerator.TransformText() at string Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpModelGenerator.ProcessTemplate(ITextTransformation transformation) at ScaffoldedModel Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpModelGenerator.GenerateModel(IModel model, ModelCodeGenerationOptions options) at ScaffoldedModel RevEng.Core.ReverseEngineerScaffolder.ScaffoldModel(string connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions, bool removeNullableBoolDefaults, bool excludeNavigations, bool dbContextOnly, bool entitiesOnly, bool useSchemaFolders) in C:/Code/Github/EFCorePowerTools/src/GUI/RevEng.Core/ReverseEngineerScaffolder.cs:line 374 at SavedModelFiles RevEng.Core.ReverseEngineerScaffolder.GenerateDbContext(ReverseEngineerCommandOptions options, List schemas, string outputContextDir, string modelNamespace, string contextNamespace) in C:/Code/Github/EFCorePowerTools/src/GUI/RevEng.Core/ReverseEngineerScaffolder.cs:line 99 at ReverseEngineerResult RevEng.Core.ReverseEngineerRunner.GenerateFiles(ReverseEngineerCommandOptions options) in C:/Code/Github/EFCorePowerTools/src/GUI/RevEng.Core/ReverseEngineerRunner.cs:line 93 at int EfReveng.Program.Main(string[] args) in C:/Code/Github/EFCorePowerTools/src/GUI/efreveng/Program.cs:line 68

at ReverseEngineerResult EFCorePowerTools.Handlers.ReverseEngineer.ResultDeserializer.BuildResult(string output) at async Task<ReverseEngineerResult> EFCorePowerTools.Handlers.ReverseEngineer.EfRevEngLauncher.GetOutputAsync() at async Task<ReverseEngineerResult> EFCorePowerTools.Handlers.ReverseEngineer.EfRevEngLauncher.LaunchExternalRunnerAsync(ReverseEngineerOptions options, CodeGenerationMode codeGenerationMode, Project project) at async Task EFCorePowerTools.Handlers.ReverseEngineer.ReverseEngineerHandler.GenerateFilesAsync(Project project, ReverseEngineerOptions options, Tuple<bool, string> containsEfCoreReference) at async Task EFCorePowerTools.Handlers.ReverseEngineer.ReverseEngineerHandler.ReverseEngineerCodeFirstAsync(Project project, string optionsPath, bool onlyGenerate, bool fromSqlProj)

aespitia avatar Sep 20 '22 16:09 aespitia

Are you using the T4 templates. Try to rename / delete the CodeTemplates folder.

ErikEJ avatar Sep 20 '22 16:09 ErikEJ

Ping?

ErikEJ avatar Sep 21 '22 11:09 ErikEJ

Are you using the T4 templates. Try to rename / delete the CodeTemplates folder.

from what i can tell, i am not using T4 templates. i have a working model if generated using the EF6 option, this error comes from the EF7 preview option when reverse engineering.

i don't see a codeTemplates folder to remove.

I'm using EF Core Power Tools 2.5.1142.0 on VS 2022

aespitia avatar Sep 21 '22 12:09 aespitia

I am unable to repro locally, can you share the database schema, for example via a .dacpac and your efpt.config.json file?

ErikEJ avatar Sep 21 '22 13:09 ErikEJ

I've removed the tables, but there are tables in 3 different schemas, as well as views, functions, and stored procs. object types 0, 1, 2, and 3.

{ "CodeGenerationMode": 3, "ContextClassName": "Context", "ContextNamespace": "", "DefaultDacpacSchema": null, "FilterSchemas": false, "IncludeConnectionString": false, "ModelNamespace": null, "OutputContextPath": "", "OutputPath": "Generated", "PreserveCasingWithRegex": true, "ProjectRootNamespace": "Project.Domain", "Schemas": null, "SelectedHandlebarsLanguage": 0, "SelectedToBeGenerated": 0, "UiHint": "SERVER.DBNAME", "UseBoolPropertiesWithoutDefaultSql": true, "UseDatabaseNames": false, "UseDbContextSplitting": true, "UseFluentApiOnly": false, "UseHandleBars": false, "UseHierarchyId": false, "UseInflector": true, "UseLegacyPluralizer": true, "UseManyToManyEntity": false, "UseNoConstructor": false, "UseNoDefaultConstructor": false, "UseNoNavigations": false, "UseNoObjectFilter": false, "UseNodaTime": false, "UseNullableReferences": true, "UseSchemaFolders": false, "UseSpatial": false, "UseT4": false }

aespitia avatar Sep 21 '22 13:09 aespitia

I need the database schema (either .sql script or bacpac file)

ErikEJ avatar Sep 21 '22 13:09 ErikEJ

I can't send the schema, but i'll see if I can recreate it in a dummy schema in a new project

aespitia avatar Sep 21 '22 13:09 aespitia

dbproject.zip

this project seems to throw the error for me if i try pointing to the dacpac

aespitia avatar Sep 21 '22 14:09 aespitia

With the same error? I am not able to repro that (.dacpac functionality is currently borken with EF Core 7)

ErikEJ avatar Sep 22 '22 05:09 ErikEJ

Alos not able to repro if I publish the database project

ErikEJ avatar Sep 22 '22 06:09 ErikEJ

OK, yes, I got a different error from the dacpac, not the same as the original. will tweak the sample project some more to see if I can narrow down the cause.

aespitia avatar Sep 22 '22 13:09 aespitia

Yeah, sorry, but .dacpac support for EF Core 7 is currently pending

ErikEJ avatar Sep 22 '22 13:09 ErikEJ

Is there any log or output you can turn on to see what the progress was to see what the last thing was that it was trying to reverse engineer?

aespitia avatar Sep 22 '22 13:09 aespitia

No, once you figure out (or share the full schema privately) I will try to address this with the EF Core team (lack of good error message)

ErikEJ avatar Sep 22 '22 13:09 ErikEJ

You could try to use the EF Core 7 command line tool, and run it with verbose logging.

ErikEJ avatar Sep 22 '22 13:09 ErikEJ

Any news?

ErikEJ avatar Sep 26 '22 06:09 ErikEJ

dbproject.zip Yes, try this one the issue is with the no-pk table. it has an FK, but no PK. will update the db on our end to see if it fixes the issue for me to get past this.

aespitia avatar Sep 26 '22 14:09 aespitia

yes fixing the PK seems to fix it, the .net core 6 implementation does show the warning in the GUI over the table, but processes it fine

aespitia avatar Sep 26 '22 14:09 aespitia

@bricelam potential EF Core 7 issue?

ErikEJ avatar Sep 26 '22 14:09 ErikEJ

Could you share the EF 6 warning?

ErikEJ avatar Sep 26 '22 14:09 ErikEJ

i just meant that in the GUI of ef Core POwertools there's a warning sign (yellow triangle) on the table that has no PK. but it processes it anyway.

aespitia avatar Sep 26 '22 14:09 aespitia

@aespitia Unable to repro based on your latest .zip file

ErikEJ avatar Oct 10 '22 07:10 ErikEJ

Closing as no repro was provided

ErikEJ avatar Oct 11 '22 12:10 ErikEJ

I happen to come across the same problem.

I have an app which uses some tables that don´t have a primary key.

When I do a reverse engineer with Ef Core 6, it works fine and generates the model correctly.

But when I choose Ef Core 7 the model generation fails with this error:

System.InvalidOperationException: Reverse engineer error: 
System.NullReferenceException: Object reference not set to an instance of an object.
   at string Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpDbContextGenerator.TransformText()
   at string Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpModelGenerator.ProcessTemplate(ITextTransformation transformation)
   at ScaffoldedModel Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpModelGenerator.GenerateModel(IModel model, ModelCodeGenerationOptions options)
   at ScaffoldedModel RevEng.Core.ReverseEngineerScaffolder.ScaffoldModel(string connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions, bool removeNullableBoolDefaults, bool excludeNavigations, bool dbContextOnly, bool entitiesOnly, bool useSchemaFolders) in C:/Code/Github/EFCorePowerTools/src/GUI/RevEng.Core/ReverseEngineerScaffolder.cs:line 385
   at SavedModelFiles RevEng.Core.ReverseEngineerScaffolder.GenerateDbContext(ReverseEngineerCommandOptions options, List<string> schemas, string outputContextDir, string modelNamespace, string contextNamespace, string projectPath, string outputPath) in C:/Code/Github/EFCorePowerTools/src/GUI/RevEng.Core/ReverseEngineerScaffolder.cs:line 103
   at ReverseEngineerResult RevEng.Core.ReverseEngineerRunner.GenerateFiles(ReverseEngineerCommandOptions options) in C:/Code/Github/EFCorePowerTools/src/GUI/RevEng.Core/ReverseEngineerRunner.cs:line 88
   at async Task<int> EfReveng.Program.Main(string[] args) in C:/Code/Github/EFCorePowerTools/src/GUI/efreveng/Program.cs:line 69

   at ReverseEngineerResult EFCorePowerTools.Handlers.ReverseEngineer.ResultDeserializer.BuildResult(string output)
   at async Task<ReverseEngineerResult> EFCorePowerTools.Handlers.ReverseEngineer.EfRevEngLauncher.GetOutputAsync()
   at async Task<ReverseEngineerResult> EFCorePowerTools.Handlers.ReverseEngineer.EfRevEngLauncher.LaunchExternalRunnerAsync(ReverseEngineerOptions options, CodeGenerationMode codeGenerationMode, Project project)
   at async Task EFCorePowerTools.Handlers.ReverseEngineer.ReverseEngineerHandler.GenerateFilesAsync(Project project, ReverseEngineerOptions options, Tuple<bool, string> containsEfCoreReference, bool onlyGenerate)
   at async Task EFCorePowerTools.Handlers.ReverseEngineer.ReverseEngineerHandler.ReverseEngineerCodeFirstAsync(Project project, string optionsPath, bool onlyGenerate, bool fromSqlProj)

I suspect the problem has to do with the tables without primary key because when I remove those tables from the reverse engineer process, it works.

blogcraft avatar Jan 19 '23 22:01 blogcraft

Digging around, it seems that the problem is related to this and not a Power tools Issue?

https://github.com/dotnet/efcore/issues/29516

blogcraft avatar Jan 19 '23 22:01 blogcraft

@blogcraft correct. And will be fixed in 7.0.3

ErikEJ avatar Jan 20 '23 03:01 ErikEJ

yay! glad to know this issue helped someone

aespitia avatar Jan 20 '23 14:01 aespitia