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 scaffold missing IndexerProperty for M2M relation after upgrading from EF Core 6 to EF Core 8 #35524

Open
stefioan opened this issue Jan 24, 2025 · 2 comments

Comments

@stefioan
Copy link

Bug description

We recently upgraded from EF Core 6 to EF Core 8, and we realised that we are missing an IndexerProperty in scaffolded DBContext for many to many relation.

DB Context code snippet:

 entity.HasMany(d => d.AnnualValues).WithMany(p => p.AcademicYears)
     .UsingEntity<Dictionary<string, object>>(
         "AnnualValueAcademicYearMapping",
         r => r.HasOne<AnnualValue>().WithMany()
             .HasForeignKey("LearnAimRef", "EffectiveFrom")
             .HasConstraintName("FK_dbo_AnnualValueAcademicYearMapping_AnnualValue"),
         l => l.HasOne<AcademicYearLookup>().WithMany()
             .HasForeignKey("AcademicYear")
             .HasConstraintName("FK_dbo_AnnualValueAcademicYearMapping_AcademicYear_Lookup"),
         j =>
         {
             j.HasKey("AcademicYear", "LearnAimRef", "EffectiveFrom").HasName("PK_dbo_AnnualValueAcademicYearMapping");
             j.ToTable("AnnualValue_AcademicYear_Mapping", "dbo");
             j.IndexerProperty<string>("AcademicYear")
                 .HasMaxLength(4)
                 .IsUnicode(false);
             j.IndexerProperty<string>("LearnAimRef")
                 .HasMaxLength(8)
                 .IsUnicode(false);
	    
             // Manually added in EF 8.0.12
             j.IndexerProperty<DateOnly>("EffectiveFrom").HasColumnType("date");
         }); 

The j.IndexerProperty("EffectiveFrom").HasColumnType("date"); is not automatically generated which is causing failures, so it needs to be added manually.

Unit test error message:
System.InvalidOperationException : The relationship from 'AnnualValueAcademicYearMapping (Dictionary<string, object>)' to 'AnnualValue' with foreign key properties {'LearnAimRef' : string, 'EffectiveFrom' : int} cannot target the primary key {'LearnAimRef' : string, 'EffectiveFrom' : DateOnly} because it is not compatible. Configure a principal key or a set of foreign key properties with compatible types for this relationship.

We couldn't find anything in the documentation, but there was a similar issue in EF Core 7 that has been resolved #32936 . Can you please advise? Thanks in advance.

Your code

[dbo].[AnnualValue](
    [LearnAimRef]   VARCHAR (8)    NOT NULL,
    [EffectiveFrom] DATE           NOT NULL,
CONSTRAINT [PK_dbo_AnnualValue] PRIMARY KEY CLUSTERED ([LearnAimRef] ASC, [EffectiveFrom] ASC) WITH (FILLFACTOR = 90))

[dbo].[AcademicYear_Lookup](
	[AcademicYear] [varchar](4) NOT NULL,
	[AcademicYearDesc] [varchar](150) NULL,
	[AcademicYearDesc2] [varchar](100) NULL,
    CONSTRAINT [PK_dbo_AcademicYear_Lookup] PRIMARY KEY ([AcademicYear]))

[dbo].[AnnualValue_AcademicYear_Mapping] (
    [AcademicYear]                  VARCHAR (4)    NOT NULL,
    [LearnAimRef]                   VARCHAR (8)    NOT NULL,
    [EffectiveFrom]                 DATE           NOT NULL,
    CONSTRAINT [PK_dbo_AnnualValueAcademicYearMapping] PRIMARY KEY CLUSTERED ([AcademicYear] ASC, [LearnAimRef] ASC, [EffectiveFrom] ASC) WITH (FILLFACTOR = 90),
    CONSTRAINT [FK_dbo_AnnualValueAcademicYearMapping_AnnualValue] FOREIGN KEY ([LearnAimRef],  [EffectiveFrom]) REFERENCES  [dbo].[AnnualValue] ([LearnAimRef], [EffectiveFrom]) ON DELETE CASCADE,
    CONSTRAINT [FK_dbo_AnnualValueAcademicYearMapping_AcademicYear_Lookup] FOREIGN KEY ([AcademicYear]) REFERENCES  [dbo].[AcademicYear_Lookup] ([AcademicYear])  ON DELETE CASCADE
    );

Stack traces


Verbose output


EF Core version

8.0.12

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.Net 8.0

Operating system

Windows 11

IDE

Visual Studio 2022 17.12.3

@AndriySvyryd
Copy link
Member

Could you also include the declarations of the related entity types and preferably put together a small runnable project?

@stefioan
Copy link
Author

Of course, I have created a small project similar to the actual implementation so you should be able to find everything you need there.
EntityFrameworkIssue.zip

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

3 participants