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

3.0 Upgrade - Entity with HasNoKey() (formerly a query type) tries to create tables when adding migration #18116

Closed
hallidev opened this issue Sep 28, 2019 · 8 comments

Comments

@hallidev
Copy link

I have the following Entity which used to be a query type:

modelBuilder
    .Entity<ModQueueEntry>()
    .HasNoKey();

As a query type it caused no issues, but now that it's an entity, migrations are trying to create tables for it.

I have several entities defined like so, none of which attempt table generation when adding a migration. I assume it's because of the ToView():

modelBuilder
    .Entity<AreaUserInfo>()
    .HasNoKey()
    .ToView("v_AreaUserInfos");

The problem is that my above entity types come out of a stored procedure. It's not a View or a Table.

I don't want to have to manually modify every migration going forward to account for this. Is there a way to prevent table generation when creating migrations?

Further technical details

EF Core version: 3.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.3

@bricelam
Copy link
Contributor

bricelam commented Oct 1, 2019

Related to #17270 and #2725.

For now, you can just use something like .ToView("You forgot to use FromSql with ModQueueEntry")

@ajcvickers
Copy link
Member

Note from triage: closing this as duplicate since the work here is covered by #17270 and #2725.

@be4i
Copy link

be4i commented Feb 23, 2021

I am seeing this behavior again with 5.0, ToView(null) is my workaround for now.

builder.Entity<ProductMediaOrdered>(typeBuilder => 
	typeBuilder.HasNoKey();
	typeBuilder.ToInMemoryQuery(() => ...);
)

@francesconi
Copy link

francesconi commented Feb 26, 2021

Experiencing a similar issue. I've used .HasNoKey().ToView(null) in order to avoid a table creation. Since version 5.0 it now generates a migration with a drop table even if there does not exist one!

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Mar 5, 2021

Call ToTable((string?)null) if you don't want the entity type to be mapped to a table, or call ToTable("MyTable", t => t.ExcludeFromMigrations()) if you want it to be ignored by migrations

@BADF00D
Copy link

BADF00D commented May 6, 2021

For some reason I don't know, using the [Keyless] Attribute instead on HasNotKey solved the problem for me

@KillerBoogie
Copy link

KillerBoogie commented Feb 17, 2022

@AndriySvyryd : I'm using EF Core 6. ToTable(null) results in

CS0121 The call is ambiguous between the following methods or properties: 'RelationalEntityTypeBuilderExtensions.ToTable<TEntity>(EntityTypeBuilder<TEntity>, string?)' and 'RelationalEntityTypeBuilderExtensions.ToTable<TEntity>(EntityTypeBuilder<TEntity>, Action<TableBuilder<TEntity>>)'

I tried .ToTable(null, t => t.ExcludeFromMigrations())
This generated a DropTable in the Up and a CreateTable in the Down.

@AndriySvyryd
Copy link
Member

@KillerBoogie Use the view name it was mapped to before: ToTable("MyView", t => t.ExcludeFromMigrations())

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 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

8 participants