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 moving to EF Core 7 #29634

Closed
SrdjanPaunovic opened this issue Nov 21, 2022 · 4 comments
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

@SrdjanPaunovic
Copy link

After moving to version 7, we are now missing IndexerProperty in scaffolded DBContext for many to many relations.

DbContext, the problematic part of code:

entity.HasMany(d => d.MockupsNavigation).WithMany(p => p.Users)
    .UsingEntity<Dictionary<string, object>>(
        "UserMockup",
        r => r.HasOne<Mockup>().WithMany()
            .HasForeignKey("MockupId")
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("user_mockup_mockup_id_fkey"),
        l => l.HasOne<User>().WithMany()
            .HasForeignKey("UserId")
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("user_mockup_user_id_fkey"),
        j =>
        {
            j.HasKey("UserId", "MockupId").HasName("user_mockup_pkey");
            j.ToTable("user_mockup");

            //Missing code in EF7
            //j.IndexerProperty<long>("UserId").ValueGeneratedOnAdd().HasColumnName("user_id");
            //j.IndexerProperty<long>("MockupId").ValueGeneratedOnAdd().HasColumnName("mockup_id");
        });

We are getting an error on SaveChangesAsync when we try to connect user with mockup instance.

Exception message

PostgresException: 42703: column "MockupId" of relation "user_mockup" does not exist

After adding these 2 missing lines everything works fine.
We couldn't find in the documentation that something has been changed compared to the previous version.
Is there any configuration parameter that we need to add now to have the previous behavior?

Include provider and version information

EF Core version: 7.0
Database provider: PostgreSQL
Target framework: NET 7.0
Operating system:
IDE: (e.g. Visual Studio 2022 17.4)

@ajcvickers
Copy link
Member

/cc @bricelam

@filipbekic01
Copy link

I confirm same problem on our end.

@miroljub1995
Copy link

Can we expect fix for this anytime soon? This breaks our scaffold.

@ajcvickers ajcvickers added this to the 7.0.x milestone Nov 30, 2022
bricelam added a commit to bricelam/efcore that referenced this issue Dec 7, 2022
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 7, 2022
@bricelam
Copy link
Contributor

bricelam commented Dec 7, 2022

🩹 Workaround

Looks like you can work around it by using custom templates and adding the following to DbContext.t4 under the code that generates j.HasIndex.

            foreach (var property in joinEntityType.GetProperties())
            {
                var propertyFluentApiCalls = property.GetFluentApiCalls(annotationCodeGenerator);
                if (propertyFluentApiCalls == null)
                {
                    continue;
                }

                usings.AddRange(propertyFluentApiCalls.GetRequiredUsings());
#>
                        j.IndexerProperty<<#= code.Reference(property.ClrType) #>>(<#= code.Literal(property.Name) #>)<#= code.Fragment(propertyFluentApiCalls, indent: 7) #>;
<#
            }

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

No branches or pull requests

5 participants