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

DbContext scaffolding creates reference error CS0120 in nameof() for InverseProperty in certain conditions #26588

Closed
janschreier opened this issue Nov 9, 2021 · 5 comments · Fixed by #26622
Assignees
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@janschreier
Copy link

when table names in n:m relations meet certain criteria, the below code is created by dbScaffold (DDL statements at the bottom)

namespace ScaffoldError.database
{
    public partial class MA
    {
        public MA()
        {
            MFMA = new HashSet<MFMA>();
        }

        [Key]
        public int MAId { get; set; }
        [StringLength(50)]
        [Unicode(false)]
        public string? Bezeichnung { get; set; }

        [ForeignKey("MAId")]
        [InverseProperty(nameof(database.MFMA.MA))]
        public virtual ICollection<MFMA> MFMA { get; set; }
    }
}

The problem is line [InverseProperty(nameof(MFMA.MA))] which wants to reference the class called MFMA but actually references the next line's Icollection. this creates compiler error CS0120 "An object reference is required for the non-static field, method, or property 'MFMA.MA'"

The solution is to qualify the MFMA like so [InverseProperty(nameof(database.MFMA.MA))]
I could imagine the issue comes from the naming pattern that one table ends with the other tables names but that's just a guess.

Statement used for scaffolding

dotnet ef dbcontext scaffold "data source=localhost;initial catalog=dbname;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" Microsoft.EntityFrameworkCore.SqlServer `--output-dir "database" --table ma --table mfma --table mfmama --data-annotations --use-database-names --force --no-onconfiguring --no-pluralize 

DDL-Statements:

CREATE TABLE dbo.MA (
  MAId INT IDENTITY
 ,Bezeichnung VARCHAR(50) NULL
 ,CONSTRAINT Pk_ma PRIMARY KEY CLUSTERED (MAId)
) ON [PRIMARY]
GO

CREATE TABLE dbo.MFMA (
  MFMAId INT IDENTITY
 ,Bezeichnung VARCHAR(50) NULL
 ,CONSTRAINT pk_mfma PRIMARY KEY CLUSTERED (MFMAId)
) ON [PRIMARY]
GO

CREATE TABLE dbo.MFMAMA (
  MAId INT IDENTITY
 ,MFMAId INT NOT NULL
 ,CONSTRAINT PK_MFMAMA PRIMARY KEY CLUSTERED (MAId, MFMAId)
) ON [PRIMARY]
GO

ALTER TABLE dbo.MFMAMA
ADD CONSTRAINT FK_MFMAMA_MAId FOREIGN KEY (MAId) REFERENCES dbo.MA (MAId)
GO

ALTER TABLE dbo.MFMAMA
ADD CONSTRAINT FK_MFMAMA_MFMAId FOREIGN KEY (MFMAId) REFERENCES dbo.MFMA (MFMAId)
GO

provider and version information

EF Core version: 6.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 6.0
Operating system:
IDE: (e.g. Visual Studio 20202 current)

@ajcvickers
Copy link
Member

ajcvickers commented Nov 9, 2021

Note for triage: regression in 6.0.

5.0 generates:

        [InverseProperty("MA")]
        public virtual ICollection<MFMAMA> MFMAMA { get; set; }

While 6.0 generates:

        [InverseProperty(nameof(MFMA.MA))]
        public virtual ICollection<MFMA> MFMA { get; set; }

@ajcvickers
Copy link
Member

Notes from triage:

  • For 7.0, we will revert the change to use nameof since it is complex to get all the scoping rules correct.
  • We may consider patching this issue based on feedback. For now, the workaround is to manually (e.g. regex search and replace) the scaffolded code.

@janschreier
Copy link
Author

I would prefer this as a patch but since this is a compile time issue I think we could live with a workaround for a year if it is too much effort to fix it now

smitpatel added a commit that referenced this issue Nov 11, 2021
Scoping causes clash in names.

Resolves #26588
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 11, 2021
@ghost ghost closed this as completed in #26622 Nov 11, 2021
ghost pushed a commit that referenced this issue Nov 11, 2021
Scoping causes clash in names.

Resolves #26588
@HakanL
Copy link

HakanL commented Jan 17, 2022

@ajcvickers I'd love to have this as a patch to 6 since manually patching the generated files (or adding a search/replace process) is cumbersome and error-prone. Nameof is a great feature, but in generated code it's less valuable since you won't refactor generated code (which I understand is why you revert the changes, good).

@ajcvickers ajcvickers removed this from the 7.0.0 milestone Jan 18, 2022
@ajcvickers ajcvickers reopened this Jan 18, 2022
@ajcvickers ajcvickers added this to the 6.0.x milestone Jan 18, 2022
@ajcvickers
Copy link
Member

Note from triage: patch this in 6.0.

@ajcvickers ajcvickers removed the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 18, 2022
smitpatel added a commit that referenced this issue Jan 20, 2022
Scoping causes clash in names.

Resolves #26588
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 20, 2022
smitpatel added a commit that referenced this issue Jan 20, 2022
Scoping causes clash in names.

Resolves #26588
@smitpatel smitpatel modified the milestones: 6.0.x, 6.0.3 Jan 26, 2022
@ajcvickers ajcvickers changed the title dbscaffold creates reference error CS0120 in nameof() for InverseProperty in certain conditions DbContext scaffolding creates reference error CS0120 in nameof() for InverseProperty in certain conditions Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants