efcore: scaffolded one-to-many property names generate with incorrect name

dotnet --version 8.0.101

dotnet ef --version Entity Framework Core .NET Command-line Tools 8.0.1

scaffolding with “dotnet ef dbcontext scaffold …”

with v7 of the framework, it would generate the following:

public partial class PlatformIdentifierMapping
{
    public Guid Id { get; set; }
    public short PlatformMappingTypeId { get; set; }
    public Guid SourcePlatformId { get; set; }
    public short SourcePlatformTypeId { get; set; }
    public Guid DestinationPlatformId { get; set; }
    public short DestinationPlatformTypeId { get; set; }
    public string MappingCode { get; set; }
    public string SourceValue { get; set; }
    public string DestinationValue { get; set; }
    public DateTimeOffset CreatedUtc { get; set; }
    public string CreatedUser { get; set; }
    public DateTimeOffset? UpdatedUtc { get; set; }
    public string UpdatedUser { get; set; }

    public virtual PlatformPlatformType DestinationPlatform { get; set; }          // <-- prior/desired name
    public virtual PlatformMappingType PlatformMappingType { get; set; }
    public virtual PlatformPlatformType SourcePlatform { get; set; }               // <-- prior/desired name
}

but after upgrading to 8.0, it now scaffolds as:

public partial class PlatformIdentifierMapping
{
    public Guid Id { get; set; }
    public short PlatformMappingTypeId { get; set; }
    public Guid SourcePlatformId { get; set; }
    public short SourcePlatformTypeId { get; set; }
    public Guid DestinationPlatformId { get; set; }
    public short DestinationPlatformTypeId { get; set; }
    public string MappingCode { get; set; }
    public string SourceValue { get; set; }
    public string DestinationValue { get; set; }
    public DateTimeOffset CreatedUtc { get; set; }
    public string CreatedUser { get; set; }
    public DateTimeOffset? UpdatedUtc { get; set; }
    public string UpdatedUser { get; set; }

    public virtual PlatformMappingType PlatformMappingType { get; set; }
    public virtual PlatformPlatformType PlatformPlatformType { get; set; }           // <-- new/wrong name
    public virtual PlatformPlatformType PlatformPlatformTypeNavigation { get; set; } // <-- new/wrong name
}

with the following SQL:

CREATE TABLE [Platform_PlatformType](
	[PlatformId] [uniqueidentifier] NOT NULL,
	[PlatformTypeId] [smallint] NOT NULL,
 CONSTRAINT [PK_Platform_PlatformType] PRIMARY KEY CLUSTERED 
(
	[PlatformId] ASC,
	[PlatformTypeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [Platform_PlatformType]  WITH CHECK ADD  CONSTRAINT [FK_Platform_PlatformType_Platform] FOREIGN KEY([PlatformId])
REFERENCES [Platform] ([Id])
GO

ALTER TABLE [Platform_PlatformType] CHECK CONSTRAINT [FK_Platform_PlatformType_Platform]
GO

ALTER TABLE [Platform_PlatformType]  WITH CHECK ADD  CONSTRAINT [FK_Platform_PlatformType_PlatformType] FOREIGN KEY([PlatformTypeId])
REFERENCES [PlatformType] ([Id])
GO

ALTER TABLE [Platform_PlatformType] CHECK CONSTRAINT [FK_Platform_PlatformType_PlatformType]
GO

CREATE TABLE [PlatformIdentifierMapping](
	[Id] [uniqueidentifier] NOT NULL,
	[PlatformMappingTypeId] [smallint] NOT NULL,
	[SourcePlatformId] [uniqueidentifier] NOT NULL,
	[SourcePlatformTypeId] [smallint] NOT NULL,
	[DestinationPlatformId] [uniqueidentifier] NOT NULL,
	[DestinationPlatformTypeId] [smallint] NOT NULL,
	[MappingCode] [varchar](50) NULL,
	[SourceValue] [varchar](256) NOT NULL,
	[DestinationValue] [varchar](256) NOT NULL,
	[CreatedUtc] [datetimeoffset](7) NOT NULL,
	[CreatedUser] [varchar](128) NOT NULL,
	[UpdatedUtc] [datetimeoffset](7) NULL,
	[UpdatedUser] [varchar](128) NULL,
 CONSTRAINT [PK_PlatformIdentifierMapping] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [PlatformIdentifierMapping] ADD  CONSTRAINT [DF_PlatformIdentifierMapping_Id]  DEFAULT (newid()) FOR [Id]
GO

ALTER TABLE [PlatformIdentifierMapping]  WITH CHECK ADD  CONSTRAINT [FK_PlatformIdentifierMapping_PlatformMappingType] FOREIGN KEY([PlatformMappingTypeId])
REFERENCES [PlatformMappingType] ([Id])
GO

ALTER TABLE [PlatformIdentifierMapping] CHECK CONSTRAINT [FK_PlatformIdentifierMapping_PlatformMappingType]
GO

ALTER TABLE [PlatformIdentifierMapping]  WITH CHECK ADD  CONSTRAINT [FK_ProposalDetail_Platform_PlatformType_DestinationPlatform] FOREIGN KEY([DestinationPlatformId], [DestinationPlatformTypeId])
REFERENCES [Platform_PlatformType] ([PlatformId], [PlatformTypeId])
GO

ALTER TABLE [PlatformIdentifierMapping] CHECK CONSTRAINT [FK_ProposalDetail_Platform_PlatformType_DestinationPlatform]
GO

ALTER TABLE [PlatformIdentifierMapping]  WITH CHECK ADD  CONSTRAINT [FK_ProposalDetail_Platform_PlatformType_SourcePlatform] FOREIGN KEY([SourcePlatformId], [SourcePlatformTypeId])
REFERENCES [Platform_PlatformType] ([PlatformId], [PlatformTypeId])
GO

ALTER TABLE [PlatformIdentifierMapping] CHECK CONSTRAINT [FK_ProposalDetail_Platform_PlatformType_SourcePlatform]
GO


About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 10
  • Comments: 25 (12 by maintainers)

Commits related to this issue

Most upvoted comments

We discussed this at length, with the three options on the table being:

  • Revert the change made in EF8. This means that the bad names generated by the code will still be generated. It also means adding back an obscure rule, making it harder to know what names to expect.
  • Add additional complexity to the rules such that the bad names don’t get generated, but the ones that make sense do. This would have to be a heuristic, and will probably still result in bad names in some cases.
  • Do nothing. Keeps the rules simple going forward. People have the T4 templates to change the behavior, or they can use Erik’s Power Tools.

Ultimately, we decided on the third option; do nothing. This keeps the rules simple and avoids us creating ridiculous names. This seems like a good default. The customization options (T4) seem like appropriate way to implement more complex naming rules, and in addition the @ErikEJ has provided a community solution with optional behavior built-in.

We will document this as a breaking change.

The fix is now in EF Core Power Tools

@ErikEJ @hallidev @bclementfidx I will discuss with the team reverting/tweaking this change in an 8.0.x patch.

@hallidev Please share a sample create script that demonstrates the issue, and I might have a look.