Efcore: EFCore 5rc - System.InvalidOperationException: 'Include property XXX not found.'

Created on 16 Oct 2020  路  2Comments  路  Source: dotnet/efcore

I had to migrate an old project which was created with EF4, so I have to use the Scaffold command:
Scaffold-DbContext "Server=MySqlServer;Database=myDatabase;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

First I migrated to EF Core 3.1.9, had to fix some ComplexTypes but it worked.
Then I tried to migrate to EF Core 5.0.0-rc.2.20475.6, the creation of the models works, but when I try to use it without custom edits, it throws an exception: System.InvalidOperationException: 'Include property 'SpaltenSpalteMitProzent.HABENKontoVB' not found.'

Here is the generated code of SpaltenSpalteMitProzent.HABENKontoVB:

modelBuilder.Entity<SpaltenSpalteMitProzent>(entity =>
{
    entity.ToTable("Spalten_SpalteMitProzent");
    entity.HasIndex(e => e.Prozent, "NonClusteredIndex-20190328-144641")
        .IncludeProperties(new[] { "Id", "HABENKontoVB" });
    entity.Property(e => e.Id).ValueGeneratedNever();
    entity.Property(e => e.HabenkontoVb).HasColumnName("HABENKontoVB");
    entity.Property(e => e.Iva).HasColumnName("IVA");
    entity.Property(e => e.SollkontoOb).HasColumnName("SOLLKontoOB");
    entity.Property(e => e.SollkontoVb).HasColumnName("SOLLKontoVB");
    entity.HasOne(d => d.IdNavigation)
        .WithOne(p => p.SpaltenSpalteMitProzent)
        .HasForeignKey<SpaltenSpalteMitProzent>(d => d.Id)
        .OnDelete(DeleteBehavior.ClientSetNull)
        .HasConstraintName("FK_SpalteMitProzent_inherits_Spalte");
});

I did some tests, if I simply remove HABENKontoVB from IncludeProperties, it works.

Edit:
By changing the code followings, it works:

    entity.HasIndex(e => e.Prozent, "NonClusteredIndex-20190328-144641")
        .IncludeProperties(e => new { 
            e.Id,
            e.HabenkontoVb
        });

Lateron I figured out that I could define:

    entity.HasIndex(e => new { e.Id, e.HabenkontoVb, e.Prozent })
        .HasDatabaseName("NonClusteredIndex-20190328-144641");
closed-duplicate customer-reported

Most helpful comment

This is a known issue, see #22150 - maybe you can use the -UseDataNames option in your scaffold command.

All 2 comments

This is a known issue, see #22150 - maybe you can use the -UseDataNames option in your scaffold command.

This is a known issue, see #22150 - maybe you can use the -UseDataNames option in your scaffold command.

Thanks, Scaffold-DbContext with -UseDatabaseNames worked for me

Was this page helpful?
0 / 5 - 0 ratings