Background
I am using EF Core 3 for an application with a number of POCOs in a DbContext that I want to be created as database tables - no problem here! I use Linq queries to get data here, and life is good.
I also have some raw SQL queries and procedures for some more complex reporting. I've created POCOs for the returned data, and added to the DbContext as a DbSet:
public class FooBarContext : DbContext
{
// ...
public DbSet<FooReport> FooReport { get; set; }
// ...
}
Where FooReport looks like:
public class FooReport
{
[Key]
public int Id { get; set; }
// ...
}
The Problem / Workaround
This creates a migration for creating a new table called FooReport, which isn't what I want.
My workaround right now is to manually remove this action from the Migration that is generated, so that, in essence, I have an empty migration:
public partial class AddFooReport : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
// intentionally clear this out, so the entity isn't created / dropped as a table
// migrationBuilder.CreateTable("FooReport", ... );
}
protected override void Down(MigrationBuilder migrationBuilder)
{
// intentionally clear this out, so the entity isn't created / dropped as a table
// migrationBuilder.DropTable("FooReport");
}
}
Then I'm able to call the procedure like so:
var result = this._fooBarContext.Set<FooReport>(@"[SP_FooReport]")
.FromSqlRaw(sql)
.ToList();
This does work, but seems hacky.
I also (unsuccessfully) tried to solve this problem by adding the NotMapped decorator to the FooReport POCO, but then the query itself fails.