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

Generated Model Snapshot throws InvalidOperationException #29534

Closed
vflame opened this issue Nov 10, 2022 · 15 comments · Fixed by #29819 or #29820
Closed

Generated Model Snapshot throws InvalidOperationException #29534

vflame opened this issue Nov 10, 2022 · 15 comments · Fixed by #29819 or #29820
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@vflame
Copy link

vflame commented Nov 10, 2022

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 vflame changed the title Generated Model Snapshot throw InvalidOperationException Generated Model Snapshot throws InvalidOperationException Nov 10, 2022
@alfnielsen
Copy link

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");
                });

@ajcvickers
Copy link
Contributor

/cc @AndriySvyryd

@AndriySvyryd AndriySvyryd self-assigned this Nov 16, 2022
@ajcvickers ajcvickers added this to the 7.0.x milestone Nov 19, 2022
teraa added a commit to teraa/trace that referenced this issue Nov 30, 2022
teraa added a commit to teraa/battletrace that referenced this issue Nov 30, 2022
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 9, 2022
@AndriySvyryd AndriySvyryd removed their assignment Dec 9, 2022
@AndriySvyryd AndriySvyryd reopened this Dec 14, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.x, 7.0.3 Dec 15, 2022
@MarounMaroun
Copy link

Any updates on this?

@roji
Copy link
Member

roji commented Dec 26, 2022

@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.

@MarounMaroun
Copy link

@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?

@teraa
Copy link

teraa commented Dec 26, 2022

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.

@MarounMaroun
Copy link

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?

@ajcvickers
Copy link
Contributor

@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.

@kastroph
Copy link

kastroph commented Jan 4, 2023

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

@roji
Copy link
Member

roji commented Jan 4, 2023

@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.

@ajcvickers
Copy link
Contributor

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

@Prazdnik
Copy link

Prazdnik commented Jan 31, 2023

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

@ErikEJ
Copy link
Contributor

ErikEJ commented Jan 31, 2023

@Prazdnik Second Tuesday in Feb 2023

@Jadja
Copy link

Jadja commented Feb 21, 2023

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..

@ajcvickers ajcvickers reopened this Feb 21, 2023
@ajcvickers ajcvickers removed this from the 7.0.3 milestone Feb 21, 2023
@ajcvickers
Copy link
Contributor

@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 ajcvickers added this to the 7.0.3 milestone Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet