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.

Scaffolding command amends PK column name (intcol to intcol1)

See original GitHub issue

In my database, I have the following single table with a single-column primary key (PK) column.

create table t1 (intcol int, charcol char(20), primary key(intcol));

Running the following scaffold command results in an error which says : The property ‘intcol1’ cannot be added to the type ‘t1’ because there was no property type specified and there is no corresponding CLR property or field. To add a shadow state property the property type must be specified.

Scaffolding Command: dotnet ef dbcontext scaffold “Database=mydb; server=myserver; UID=user; PWD=password” OneDB.EntityFramework.Core -o Models --project MyApp.csproj -v

OneDB.EntityFramework.Core is the provider name.

After debugging the scaffolding code, got to know that suffix (1) is getting added from CSharpUniqueNamer.cs GetName method

public override string GetName(T item)
        {
            if (NameCache.ContainsKey(item))
            {
                return base.GetName(item);
            }
            var input = base.GetName(item);
            var name = input;
            var suffix = 1;
            while (_usedNames.Contains(name))
            {
                name = input + suffix++;
            }
            _usedNames.Add(name);
            NameCache[item] = name;
            return name;
        }

Wanted to know why are we adding this suffix, and what needs to be done to avoid this error.

Below is the stack trace:-

Microsoft.EntityFrameworkCore.Design.dll!Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpUniqueNamer<Microsoft.EntityFrameworkCore.Scaffolding.Metadata.DatabaseColumn>.GetName(Microsoft.EntityFrameworkCore.Scaffolding.Metadata.DatabaseColumn item) Line 76
Microsoft.EntityFrameworkCore.Design.dll!Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.GetPropertyName(Microsoft.EntityFrameworkCore.Scaffolding.Metadata.DatabaseColumn column) Line 176
[External Code]	
Microsoft.EntityFrameworkCore.Design.dll!Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitPrimaryKey(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder builder, Microsoft.EntityFrameworkCore.Scaffolding.Metadata.DatabaseTable table) Line 555
Microsoft.EntityFrameworkCore.Design.dll!Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitTable(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder, Microsoft.EntityFrameworkCore.Scaffolding.Metadata.DatabaseTable table) Line 352
Microsoft.EntityFrameworkCore.Design.dll!Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitTables(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder, System.Collections.Generic.ICollection<Microsoft.EntityFrameworkCore.Scaffolding.Metadata.DatabaseTable> tables) Line 309
Microsoft.EntityFrameworkCore.Design.dll!Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitDatabaseModel(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder, Microsoft.EntityFrameworkCore.Scaffolding.Metadata.DatabaseModel databaseModel) Line 201
Microsoft.EntityFrameworkCore.Design.dll!Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.Create(Microsoft.EntityFrameworkCore.Scaffolding.Metadata.DatabaseModel databaseModel, Microsoft.EntityFrameworkCore.Scaffolding.ModelReverseEngineerOptions options) Line 109
Microsoft.EntityFrameworkCore.Design.dll!Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(string connectionString, Microsoft.EntityFrameworkCore.Scaffolding.DatabaseModelFactoryOptions databaseOptions, Microsoft.EntityFrameworkCore.Scaffolding.ModelReverseEngineerOptions modelOptions, Microsoft.EntityFrameworkCore.Scaffolding.ModelCodeGenerationOptions codeOptions) Line 126
OneDBScaffoldDebug.dll!OneDBScaffoldDebug.Program.Main(string[] args) Line 43

D:>dotnet ef core --version Entity Framework Core .NET Command-line Tools 5.0.9 D:>dotnet --version 3.1.401

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ErikEJcommented, Sep 13, 2021
0reactions
ajcvickerscommented, Oct 15, 2021

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we’d like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scaffolding (Reverse Engineering) - EF Core
Reverse engineering is the process of scaffolding entity type classes and a DbContext class based on a database schema. It can be performed ......
Read more >
Scaffold-DbContext: Flag to minimally alter column/table ...
Is there a command line option to allow objects to be named as they ... Scaffold - DbContext rename entity class name and...
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