Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffolding tool is generating invalid C# code when generating many-to-many relationships #27295

Closed
mjhoffm2 opened this issue Jan 27, 2022 · 1 comment

Comments

@mjhoffm2
Copy link

mjhoffm2 commented Jan 27, 2022

In entity framework 6, the new scaffolding tool is generating many-to-many relationships differently than it did in previous versions. I would like the ability to opt out of this, as mentioned in #26820 but that is a different discussion.

I put together a very basic example that just has an Author table, a Book table, and an AuthorBook table.

When generating the InverseProperty attribute for the two tables, it generates invalid code. For example, on the "Author" collection property on the "Book" class. It generates [InverseProperty(nameof(Author.Book))]. However, this results in the error "CS0120 An object reference is required for the non-static field, method, or property 'Book.Author'"
The issue appears to be due to the fact that the generated property on this class is also called "Author", which takes precedence over the class called "Author" when resolving the identifier.

image

Here is the code I used:

CREATE TABLE [dbo].[Author](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](max) NULL,
 CONSTRAINT [PK_Author] PRIMARY KEY CLUSTERED 
 (
	[Id] ASC
 )
)
GO

CREATE TABLE [dbo].[AuthorBook](
	[AuthorId] [int] NOT NULL,
	[BookId] [int] NOT NULL,
 CONSTRAINT [PK_AuthorBook] PRIMARY KEY CLUSTERED 
 (
	[AuthorId] ASC,
	[BookId] ASC
 )
)


CREATE TABLE [dbo].[Book](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Title] [nvarchar](max) NULL,
 CONSTRAINT [PK_Book] PRIMARY KEY CLUSTERED 
 (
	[Id] ASC

 )
)
GO
ALTER TABLE [dbo].[AuthorBook]  WITH CHECK ADD  CONSTRAINT [FK_AuthorBook_Author] FOREIGN KEY([AuthorId])
REFERENCES [dbo].[Author] ([Id])
GO
ALTER TABLE [dbo].[AuthorBook] CHECK CONSTRAINT [FK_AuthorBook_Author]
GO
ALTER TABLE [dbo].[AuthorBook]  WITH CHECK ADD  CONSTRAINT [FK_AuthorBook_Book] FOREIGN KEY([BookId])
REFERENCES [dbo].[Book] ([Id])
GO
ALTER TABLE [dbo].[AuthorBook] CHECK CONSTRAINT [FK_AuthorBook_Book]
GO

Here is the Scaffolding command I used:

Scaffold-DbContext "Server=.\SQLEXPRESS;Database=test-database;Trusted_Connection=True;TrustServerCertificate=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models/Database -Context MyDbContext -f -d -v -NoPluralize

Include provider and version information

EF Core version: 6.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022

@mjhoffm2
Copy link
Author

Oops looks like this is a duplicate of #26588

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants