Entity Framework Core Interview Question #143

How do you query database views with EF Core?

Performance, Raw SQL, Stored Procedures & Advanced Queries Senior Advanced

Quick Interview Answer

A database view can be mapped to an EF Core type, often as a keyless entity type when the view has no suitable primary key. EF Core can then query the mapped type with LINQ. Keyless view mappings are generally used for read-only reporting unless a separate update strategy is intentionally designed.

Detailed Explanation

A database view can be mapped to an EF Core type, often as a keyless entity type when the view has no suitable primary key. EF Core can then query the mapped type with LINQ. Keyless view mappings are generally used for read-only reporting unless a separate update strategy is intentionally designed.

Code Example

modelBuilder.Entity<SalesSummary>(entity =>
{
    entity.HasNoKey();
    entity.ToView("vw_SalesSummary");
});