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

Keyless entity without a table - broken in 7.0 #4144

Open
RichardD2 opened this issue Nov 10, 2022 · 6 comments
Open

Keyless entity without a table - broken in 7.0 #4144

RichardD2 opened this issue Nov 10, 2022 · 6 comments

Comments

@RichardD2
Copy link

As per this thread, I have a keyless entity which is not mapped to a table or view:

public sealed class StringSplitResult
{
    public string Value { get; set; } = string.Empty;
}

...

modelBuilder.Entity<StringSplitResult>().HasNoKey().ToTable(null, t => t.ExcludeFromMigrations());

This works perfectly in EF Core 6.0.

Immediately after upgrading to 7.0, when I try to add a migration, I get:

System.ArgumentNullException: Value cannot be null. (Parameter 'name')
  at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName)
  at Microsoft.EntityFrameworkCore.RelationalEntityTypeBuilderExtensions.ToTable[TEntity](EntityTypeBuilder1 entityTypeBuilder, String name, String schema, Action1 buildAction)
  at Microsoft.EntityFrameworkCore.RelationalEntityTypeBuilderExtensions.ToTable[TEntity](EntityTypeBuilder1 entityTypeBuilder, String name, Action1 buildAction)

EF Core version: 7.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 11 22H2
IDE: Visual Studio 2022 17.4

@RichardD2
Copy link
Author

To work around this, I had to change the configuration to:

.ToTable(t => t.ExcludeFromMigrations())

I then had to modify the model snapshot and the designer for every existing migration to change:

b.ToTable((string)null, t => t.ExcludeFromMigrations());

to:

b.ToTable(t => t.ExcludeFromMigrations());

However, I have no idea whether this is the correct approach, or what side-effects this change will have.

@AndriySvyryd
Copy link
Member

This is an unfortunate side effect of dotnet/efcore#19811.
To achieve the same effect of .ToTable((string)null, t => t.ExcludeFromMigrations()); a migration needs to be created (or existing one modified) with .ToTable(t => t.ExcludeFromMigrations()); then call .ToTable((string)null); and create another migration.

We'll need to document this as a breaking change

@RichardD2
Copy link
Author

@AndriySvyryd Thanks for the reply. How would I fix it for existing migrations? Is it enough to edit them to remove the (string)null parameter? And would there be any side-effects of not having the .ToTable((string)null) call?

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Nov 16, 2022

Calling .ToTable(t => t.ExcludeFromMigrations()) instead of .ToTable((string)null) means that migrations will ignore it, but if you query the entity type then it will try to query the default table unless you have some configuration that overrides it (e.g. ToView). Also, if there are derived types in the model some validation could fail if the configuration is not compatible with the default table.

@RichardD2
Copy link
Author

@AndriySvyryd For this specific entity, it's a private nested and sealed class used to represent the results of a STRING_SPLIT call, and it's only every queried with a .FromSqlInterpolated call, so hopefully that shouldn't be a problem.

@ajcvickers
Copy link
Member

Note from triage: document this as a breaking change.

@ajcvickers ajcvickers transferred this issue from dotnet/efcore Nov 17, 2022
@ajcvickers ajcvickers added this to the 7.0.0 milestone Nov 17, 2022
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