EntityFrameWork Core 5.0 tries to drops dbset from raw queries which do not exist

520 views Asked by At

I created DbSets for my Raw Queries or StoredProcs in my dbcontext and used modelBuilder.Entity<_Entity>().HasNoKey().ToView(null) under OnModelCreating. I use them in my controller like below:

_context._RTable.FromSqlRaw("exec sp_Run @thiscasenumber", casenumber).AsNoTracking().ToList();

It used to work fine on EF Core 3.1. I recently upgraded my solution to 5.0. now when I try to do add-migration, the first time it does the job and everything works fine, but without making any change, after update-database, when I run add-migration again, it creates a migration and tries to delete these non-existing entities. migrationBuilder.DropTable(name: "_RTable");

Is there any way to prevent EF from dropping these "Tables" which do not exist?

Thanks

1

There are 1 answers

0
dside On

This is due to breaking changes in .ToView(null) behavior in .NET 5.

Based on this post you should be using .ToTable("ViewName", t => t.ExcludeFromMigrations()) to exclude enity set from migration.