You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
ALTERTABLE [dbo].[AuthorBook] WITH CHECK ADD CONSTRAINT [FK_AuthorBook_Author] FOREIGN KEY([AuthorId])
REFERENCES [dbo].[Author] ([Id])
GO
ALTERTABLE [dbo].[AuthorBook] CHECKCONSTRAINT [FK_AuthorBook_Author]
GO
ALTERTABLE [dbo].[AuthorBook] WITH CHECK ADD CONSTRAINT [FK_AuthorBook_Book] FOREIGN KEY([BookId])
REFERENCES [dbo].[Book] ([Id])
GO
ALTERTABLE [dbo].[AuthorBook] CHECKCONSTRAINT [FK_AuthorBook_Book]
GO
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.
Here is the code I used:
Here is the Scaffolding command I used:
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
The text was updated successfully, but these errors were encountered: