question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CLI is throwing Exception The key {'Id'} cannot be added to keyless type '<TableName>(Dictionary<string, object>)' although I am excluding all tables

See original GitHub issue

I wanted only few stored procs so I marked all (tables, functions, views and SPs) with filter: “exclusionWildcard”: “*” and then added [“exclude”: false] to the stored procs I wanted to reverse engineer. I get the following error complaining about this table <Table_name_in_my_DB> although I am excluding all tables. May be I am missing something here. I checked SQL and that table did not have a PK constraint. I could not do anything since I don’t have enough permissions to Alter the table and see if this solves the issue.

System.InvalidOperationException: The key {‘Id’} cannot be added to keyless type ‘<Table_name_in_my_DB>(Dictionary<string, object>)’. at Key Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddKey(IReadOnlyList<Property> properties, ConfigurationSource configurationSource) at IMutableKey Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.Microsoft.EntityFrameworkCore.Metadata.IMutableEntityType.AddKey(IReadOnlyList<IMutableProperty> properties) at IMutableForeignKey Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitForeignKey(ModelBuilder modelBuilder, DatabaseForeignKey foreignKey) at ModelBuilder Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitForeignKeys(ModelBuilder modelBuilder, IList<DatabaseForeignKey> foreignKeys) at ModelBuilder Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitDatabaseModel(ModelBuilder modelBuilder, DatabaseModel databaseModel) at IModel Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.Create(DatabaseModel databaseModel, ModelReverseEngineerOptions options) at ScaffoldedModel RevEng.Core.ReverseEngineerScaffolder.ScaffoldModel(string connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions, bool removeNullableBoolDefaults, bool dbContextOnly, bool entitiesOnly, bool useSchemaFolders) in ReverseEngineerScaffolder.cs:371 at SavedModelFiles RevEng.Core.ReverseEngineerScaffolder.GenerateDbContext(ReverseEngineerCommandOptions options, List<string> schemas, string outputContextDir, string modelNamespace, string contextNamespace , string projectPath, string outputPath) in ReverseEngineerScaffolder.cs:102 at ReverseEngineerResult RevEng.Core.ReverseEngineerRunner.GenerateFiles(ReverseEngineerCommandOptions options) in ReverseEngineerRunner.cs:81 at ReverseEngineerResult ErikEJ.EFCorePowerTools.HostedServices.ScaffoldHostedService.<>c__DisplayClass6_0.<ExecuteAsync>b__0() in ScaffoldHostedService.cs:83 at void ErikEJ.EFCorePowerTools.Services.DisplayService.<>c__DisplayClass7_01.<Wait>b__0(StatusContext ctx) in DisplayService.cs:63 at Task Spectre.Console.Status.<>c__DisplayClass14_0.<Start>b__0(StatusContext ctx) in Status.cs:44 at void Spectre.Console.Status.<>c__DisplayClass16_0.<<StartAsync>b__0>d.MoveNext() in Status.cs:79 at void Spectre.Console.Status.<>c__DisplayClass17_01.<<StartAsync>b__0>d.MoveNext() in Status.cs:120 at void Spectre.Console.Progress.<>c__DisplayClass28_0`1.<<StartAsync>b__0>d.MoveNext() in Progress.cs:133 at async Task<T> Spectre.Console.Internal.DefaultExclusivityMode.RunAsync<T>(Func<Task<T>> func) in DefaultExclusivityMode.cs:40 at async Task<T> Spectre.Console.Progress.StartAsync<T>(Func<ProgressContext, Task<T>> action) in Progress.cs:116 at async Task<T> Spectre.Console.Status.StartAsync<T>(string status, Func<StatusContext, Task<T>> func) in Status.cs:117 at async Task Spectre.Console.Status.StartAsync(string status, Func<StatusContext, Task> action) in Status.cs:77 at void Spectre.Console.Status.Start(string status, Action<StatusContext> action) in Status.cs:48 at T ErikEJ.EFCorePowerTools.Services.DisplayService.Wait<T>(string message, Func<T> doFunc) in DisplayService.cs:58 at async Task ErikEJ.EFCorePowerTools.HostedServices.ScaffoldHostedService.ExecuteAsync(CancellationToken stoppingToken) in ScaffoldHostedService.cs:81 at async Task Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken) at async Task<IHost> Microsoft.Extensions.Hosting.HostingAbstractionsHostBuilderExtensions.StartAsync(IHostBuilder hostBuilder, CancellationToken cancellationToken)

Provide steps to reproduce

Please include a complete description or project that I can redo/run to reproduce the issue.

Provide technical details

EF Core Power Tools 6.0.0-rc.2 and 7.0.0-rc.2

  • EF Core version in use: EF Core 7

  • Database engine: SQL Server

“code-generation”: { “enable-on-configuring”: false, “type”: “all”, “use-database-names”: false, “use-data-annotations”: false, “use-nullable-reference-types”: false, “use-inflector”: false, “use-legacy-inflector”: false, “use-many-to-many-entity”: false, “use-t4”: false, “remove-defaultsql-from-bool-properties”: false, “soft-delete-obsolete-files”: false, “discover-multiple-stored-procedure-resultsets-preview”: false, “use-alternate-stored-procedure-resultset-discovery”: false }

Issue Analytics

  • State:closed
  • Created 4 months ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
ErikEJcommented, Jun 3, 2023

@xheox yeah, maybe it should be skipping all tables and views, but currently it is not happening, as stored procs are not supported by the built-in scaffolding.

I think it might be an issue to skip it, as the DbContext will not be generated then. Maybe you can opt-in to scaffold a single table as a workaround by setting exclude: false for a single (dummy) table?

1reaction
ErikEJcommented, Jun 3, 2023

I suggest installing and try to run “dotnet ef dbcontext scaffold”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scaffold-DbContext fails: System. ...
System.InvalidOperationException: The key {'DocumentTypeId'} cannot be added to keyless type 'DocumentType (Dictionary<string, object>)'. at Microsoft.
Read more >
What's New in EF Core 7.0
If the table has triggers, then the call to SaveChanges in the code above will throw an exception: Unhandled exception. Microsoft.
Read more >
EF Read/Write keyless table
I have a table that does not have keys, having Id as the only non-nullable column, I have to write to it so...
Read more >
Entity Framework Core | PDF
An exception is thrown if the model has changed since the schema was ... Dictionary<string, object> ) to represent different entity types in...
Read more >
Apps and Services With . NET 7
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found