Scaffolding command amends PK column name (intcol to intcol1)
See original GitHub issueIn 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:
- Created 2 years ago
- Comments:9 (5 by maintainers)
What does your databaseModel look like?
How is your https://github.com/dotnet/efcore/blob/0ba16959c7c4f38f9162101966a6f863ba707120/src/EFCore.Relational/Scaffolding/IDatabaseModelFactory.cs implemented?
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.