EFCorePowerTools
EFCorePowerTools copied to clipboard
EF Core 7 Reverse Engineer Error
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
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)
Are you using the T4 templates. Try to rename / delete the CodeTemplates folder.
Ping?
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
I am unable to repro locally, can you share the database schema, for example via a .dacpac and your efpt.config.json file?
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 }
I need the database schema (either .sql script or bacpac file)
I can't send the schema, but i'll see if I can recreate it in a dummy schema in a new project
With the same error? I am not able to repro that (.dacpac functionality is currently borken with EF Core 7)
Alos not able to repro if I publish the database project
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.
Yeah, sorry, but .dacpac support for EF Core 7 is currently pending
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?
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)
You could try to use the EF Core 7 command line tool, and run it with verbose logging.
Any news?
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.
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
@bricelam potential EF Core 7 issue?
Could you share the EF 6 warning?
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 Unable to repro based on your latest .zip file
Closing as no repro was provided
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.
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 correct. And will be fixed in 7.0.3
yay! glad to know this issue helped someone