Efcore: Unable to map Entity to view

Created on 17 Sep 2019  ยท  3Comments  ยท  Source: dotnet/efcore

Description

I'm having trouble mapping an entity to a basic view using the new method described in 3.0. I've tried mapping it using different variations of Entity<>().ToView() and every time it gives me an invalid object name exception. I'm able to execute both of these queries correctly on my database.

select * from vSurveys
select * from dbo.vSurveys

Below are the different mappings I've tried. It can't seem to find object, regardless of whether dbo is in the name or not. Am I missing anything when it comes to mapping an entity to a view? Any help would be greatly appreciated.

Steps to reproduce

public DbSet<Survey> Surveys { get; set; }

var survey = await _context.Surveys
    .ToListAsync();
// SqlException: Invalid object name 'vSurveys'.
modelBuilder.Entity<Survey>()
    .ToView("vSurveys")
    .HasNoKey();

// SqlException: Invalid object name 'dbo.vSurveys'.
modelBuilder.Entity<Survey>()
    .ToView("dbo.vSurveys")
    .HasNoKey();

// SqlException: Invalid object name 'dbo.vSurveys'.
modelBuilder.Entity<Survey>()
    .ToView("vSurveys", "dbo")
    .HasNoKey();