ErikEJ / EFCorePowerTools

Entity Framework Core Power Tools - reverse engineering, migrations and model visualization in Visual Studio & CLI
MIT License
2.19k stars 298 forks source link

Many to many entity is scaffolded but it should be navigation property #2307

Closed janseris closed 6 months ago

janseris commented 6 months ago

obrazek

obrazek

Incorrectly generates join entity. Expected result: User has GlobalRole navigation property and the entity is not generated. obrazek

The schema goes like this (primary key consists of two foreign keys in the M:N join table): obrazek

ErikEJ commented 6 months ago

If you want free support, you must provide repro scripts, not screenshots, and you must use the latest released version of the tool.

janseris commented 6 months ago

Thanks I will try latest version

janseris commented 6 months ago

If you want free support, you must provide repro scripts, not screenshots, and you must use the latest released version of the tool.

I have been sponsoring you on GitHub for 21 months btw. This issue is not resolved in latest EF Core Power Tools version.

Many to many entity is created with both EF Core 7 and 8 settings. The settings are as shown in screenshots above.

The database script is here. The database is nothing special.

USE [DatabaseName]
GO
/****** Object:  Table [dbo].[GLOBAL_ROLE]    Script Date: 05.05.2024 23:44:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[GLOBAL_ROLE](
    [ID] [int] NOT NULL,
    [Nazev] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_GLOBAL_ROLE] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[USER]    Script Date: 05.05.2024 23:44:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[USER](
    [ID] [int] NOT NULL,
    [Jmeno] [nvarchar](50) NOT NULL,
    [Prijmeni] [nvarchar](50) NOT NULL,
    [TitulyPredJmenem] [nvarchar](10) NULL,
    [Email] [nvarchar](50) NOT NULL,
    [Firma] [nvarchar](100) NULL,
 CONSTRAINT [PK_UZIVATEL] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[V_USER__GLOBAL_ROLE]    Script Date: 05.05.2024 23:44:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[V_USER__GLOBAL_ROLE](
    [UserID] [int] NOT NULL,
    [GlobalRoleID] [int] NOT NULL,
 CONSTRAINT [PK_V_USER__GLOBAL_ROLE] PRIMARY KEY CLUSTERED 
(
    [UserID] ASC,
    [GlobalRoleID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[V_USER__GLOBAL_ROLE]  WITH CHECK ADD  CONSTRAINT [FK_V_USER__GLOBAL_ROLE_GLOBAL_ROLE] FOREIGN KEY([GlobalRoleID])
REFERENCES [dbo].[GLOBAL_ROLE] ([ID])
GO
ALTER TABLE [dbo].[V_USER__GLOBAL_ROLE] CHECK CONSTRAINT [FK_V_USER__GLOBAL_ROLE_GLOBAL_ROLE]
GO
ALTER TABLE [dbo].[V_USER__GLOBAL_ROLE]  WITH CHECK ADD  CONSTRAINT [FK_V_USER__GLOBAL_ROLE_USER] FOREIGN KEY([GlobalRoleID])
REFERENCES [dbo].[USER] ([ID])
GO
ALTER TABLE [dbo].[V_USER__GLOBAL_ROLE] CHECK CONSTRAINT [FK_V_USER__GLOBAL_ROLE_USER]
GO

Very strange that this does not happen for all M:N relations. For this relation, the code is generated correctly: obrazek

obrazek

I am not able to spot any difference between these two: obrazek

I also tried deleting EFPT config json and trying again but received the same result.

I could not reproduce with new database with isolated tables, so in attachment I am enclosing script for whole database and tables (note: files with .sql extension cannot be attached, so I changed the extension to .txt). create database.txt

add tables.txt

ErikEJ commented 6 months ago

Your sponsorship is highly appreciated.

Thanks for the scripts I will have a closer look.

ErikEJ commented 6 months ago

@janseris This foreign key is wrong, so the table is not a proper many to many table:

ALTER TABLE [dbo].[V_USER__GLOBAL_ROLE]  WITH CHECK ADD  CONSTRAINT [FK_V_USER__GLOBAL_ROLE_USER] FOREIGN KEY([GlobalRoleID])
REFERENCES [dbo].[USER] ([ID])

It should most likely be something like:

ALTER TABLE [dbo].[V_USER__GLOBAL_ROLE]  WITH CHECK ADD  CONSTRAINT [FK_V_USER__GLOBAL_ROLE_USER] FOREIGN KEY([UserID])
REFERENCES [dbo].[USER] ([ID])
ErikEJ commented 6 months ago

after making that changes, it get this:

public partial class User
{
    public int Id { get; set; }

    public string Email { get; set; }

    public virtual ICollection<GlobalRole> GlobalRoles { get; set; } = new List<GlobalRole>();
}
janseris commented 6 months ago

Ohhhhhh I am sorry. Thanks for pointing it out.

ErikEJ commented 6 months ago

No problem, there usually is a good explanation! 😄