efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Generated Model Snapshot throws InvalidOperationException

Open vflame opened this issue 4 years ago • 15 comments

When migrating from EF6 to EF7, the EF7 generated model snapshot throws System.InvalidOperationException: Table name must be specified to configure a table-specific property mapping. when being applied. The generated snapshot also prevents the migration from being removed due to the same exception.

The base type is mapped to a custom table name and the expected behavior is that types inheriting from the base table should share the same table name without explicit configuration.

Only the .csproj and nuget libraries were updated to 7, no model or configuration changes were made.

EF Core 6 generated model:

 modelBuilder.Entity("Domain.Aggregates.CustomerBalances.OutgoingCrossPlatformBalanceTransferLedgerEntry", b =>
                {
                    b.HasBaseType("Domain.Aggregates.CustomerBalances.LedgerEntry");

                    b.Property<string>("TransferId")
                        .HasColumnType("text")
                        .HasColumnName("OutgoingCrossPlatformBalanceTransferLedgerEntry_TransferId"); //moved to b.ToTable() in EF7

                    b.HasIndex("TransferId")
                        .IsUnique();

                    b.HasDiscriminator().HasValue("OutgoingCrossPlatformBalanceTransferLedgerEntry");
                });

EF Core 7 generated model:

 modelBuilder.Entity("Domain.Aggregates.CustomerBalances.OutgoingCrossPlatformBalanceTransferLedgerEntry", b =>
                {
                    b.HasBaseType("Domain.Aggregates.CustomerBalances.LedgerEntry");

                    b.Property<string>("TransferId")
                        .HasColumnType("text");

                    b.HasIndex("TransferId")
                        .IsUnique();

//b.ToTable() generated code in EF7
//exception thrown: System.InvalidOperationException: Table name must be specified to configure a table-specific property mapping.
                    b.ToTable(t =>
                        {
                            t.Property("TransferId")
                                .HasColumnName("OutgoingCrossPlatformBalanceTransferLedgerEntry_TransferId");
                        });

                    b.HasDiscriminator().HasValue("OutgoingCrossPlatformBalanceTransferLedgerEntry");
                });

Config for base type:

public class LedgerEntryConfiguration : IEntityTypeConfiguration<LedgerEntry>
{
    public void Configure(EntityTypeBuilder<LedgerEntry> builder)
    {
        builder.HasIndex(p => p.CreatedAt);
        builder.ToTable("BalanceLedgerEntries");
    }
}

By adding builder.ToTable("BalanceLedgerEntries"); in the subtype configuration, the following is generated in the snapshot, which works:

 b.ToTable("BalanceLedgerEntries", null, t =>
                        {
                            t.Property("TransferId")
                                .HasColumnName("OutgoingCrossPlatformBalanceTransferLedgerEntry_TransferId");
                        });

vflame avatar Nov 10 '22 21:11 vflame

We are getting the same error, (both with changes, but also with an empty migration) (Using TPH)

The snapshot now adds "ToTable" with property definitions, that was set on the "Property" before:

modelBuilder.Entity("Domain.Entities.Template", b =>
                {
                    b.HasBaseType("Domain.Entities.DynamicFieldListEntity");

                    b.Property<bool>("IncludeGdpr")
                        .HasColumnType("bit");

                    b.Property<string>("Name")
                        .IsRequired()
                        .HasColumnType("nvarchar(max)");

                    b.ToTable("DynamicFieldListEntity", "entities", t =>
                        {
                            t.Property("IncludeGdpr")
                                .HasColumnName("Template_IncludeGdpr");

                            t.Property("Name")
                                .HasColumnName("Template_Name");
                        });

                    b.HasDiscriminator().HasValue("Template");
                });

If we manually change this back in the snapshot, it again works:

            modelBuilder.Entity("Domain.Entities.Template", b =>
                {
                    b.HasBaseType("Domain.Entities.DynamicFieldListEntity");

                    b.Property<bool>("IncludeGdpr")
                        .HasColumnName("Template_IncludeGdpr")
                        .HasColumnType("bit");

                    b.Property<string>("Name")
                        .IsRequired()
                        .HasColumnName("Template_Name")
                        .HasColumnType("nvarchar(max)");

                    b.HasDiscriminator().HasValue("Template");
                });

alfnielsen avatar Nov 11 '22 10:11 alfnielsen

/cc @AndriySvyryd

ajcvickers avatar Nov 15 '22 15:11 ajcvickers

Any updates on this?

MarounMaroun avatar Dec 26 '22 08:12 MarounMaroun

@MarounMaroun ths fix for this will be released as part of 7.0.3, in February. In the meantime, you can use the 8.0 daily builds, which contain the fix and are otherwise very stable.

roji avatar Dec 26 '22 09:12 roji

@roji This means that I must update Microsoft.EntityFrameworkCore as well, which is really risky to run in production from a dev package. Is there a better workaround?

MarounMaroun avatar Dec 26 '22 11:12 MarounMaroun

Do we really need to wait 2 months for release cycle? I don't really have much confidence in dev builds being stable when major release introduces such regressions.

teraa avatar Dec 26 '22 11:12 teraa

This bug is a serious blocker for us. The workaround requires modifying the snapshot before creating and running migrations. Can't this be released earlier?

MarounMaroun avatar Dec 26 '22 12:12 MarounMaroun

@MarounMaroun Unfortunately not. We are constrained by the .NET release mechanics, which are complicated and long. The January release is already fully complete and cannot be changed. It was also a highly restricted release for internal reasons. As much as we would like to be able to get releases out sooner, we are currently not able to do so.

ajcvickers avatar Dec 26 '22 12:12 ajcvickers

This is a major issue for my team. Pretty please, can we get an official fix

kastroph avatar Jan 04 '23 11:01 kastroph

@kastroph this issue has already been fixed, and is in the 7.0.3 milestone - that means the fix will be part of that release, which will be out in about a month. I recommend testing with the latest daily build (which contains the fix), to make sure it works for you. You can also use that build until 7.0.3 is out - our daily builds are very stable.

roji avatar Jan 04 '23 12:01 roji

@kastroph As all the comments above indicate, the fix is approved for 7.0.3.

ajcvickers avatar Jan 04 '23 12:01 ajcvickers

@ajcvickers Tell me please, there is no exact release date for version 7.0.3?

Prazdnik avatar Jan 31 '23 06:01 Prazdnik

@Prazdnik Second Tuesday in Feb 2023

ErikEJ avatar Jan 31 '23 09:01 ErikEJ

This issue is still happening on version 7.0.3. Entities with a defined basetype seem to not be inhereting the table name correctly.

Edit: just have to rebuild the migrations created in 7.0.2, haven't got it working yet but this seems to be heading in the right direction..

Jadja avatar Feb 21 '23 14:02 Jadja

@Jadja Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

ajcvickers avatar Feb 21 '23 17:02 ajcvickers