title: EF Core: How to query a SQL View
body: <p>I have a SQL View in my DB called 'dbo.Viewtest'. This view combines the data of 2 other tables, that have identical column types. The view also adds a column to show from which table the data originated.</p>
  <p>This is what the SQL View looks like in SQL Server Object Explorer:</p>
  <h3>dbo.ViewTest</h3>
  <p>Id | Type | Content | Location</p>
  <p>1  | h1   | Welcome!| Home</p>
  <p>2  | p    | Log in  | Home</p>
  <p>3  | h2   | guide   | Home</p>
  <p>1  | h1   | info    | Events</p>
  <p>2  | p    | follow  | Events</p>
  <p>This view is created from 2 tables, one of which is called 'HomeContent' and the other 'EventsContent' (hence the location column). The view was created, using the following code:</p>
  <pre><code>protected override void Up(MigrationBuilder migrationBuilder)
          {
              migrationBuilder.Sql(&quot;CREATE VIEW ViewTest AS &quot; +
                  &quot;SELECT Id, Type, Content, 'Home' location FROM HomeContent UNION ALL &quot; +
                  &quot;SELECT Id, Type, Content, 'Evenementen' FROM EvenementenContent&quot;);
  
          }
  </code></pre>
  <p>I then added a Model Class as follows:</p>
  <pre><code>  [Table(&quot;ViewTest&quot;)]
      public class TestViewContent
      {
          [Key]
          public int Id { get; set; }
          public string Type { get; set; }
          public string Content { get; set; }
          public string Location { get; set; }
      }
  </code></pre>
  <p>And finally, i've added it to my DbContext:</p>
  <pre><code> public DbSet&lt;TestViewContent&gt; TestViewContent { get; set; }
  </code></pre>
  <p>Now, here is where things start breaking. When i try to display this SQL View, using EF Core, i get the following broken output:</p>
  <p>Id | Type | Content | Location</p>
  <p>1  | h1   | Welcome!| Home</p>
  <p>2  | p    | Log in  | Home</p>
  <p>3  | h2   | guide   | Home</p>
  <p>1  | h1   | Welcome | Home</p>
  <p>2  | p    | log in  | Home</p>
  <p>As you can see, for whatever reason when i try to display the SQL View, the data 'loops-around' when data from the 'Events' table is supposed to show. So for whatever reason, it only displays data from the home table.</p>
  <p>The code that i use for displaying the SQL View is:</p>
  <pre><code> private readonly DbApplicationContext_context;
  
          public TestViewContentController(DbApplicationContext_context)
          {
              _context = context;
          }
  
     public async Task&lt;IActionResult&gt; Index()
          {
              return View(await _context.TestViewContent.ToListAsync());
          }
  </code></pre>
  <p>Is there anyone who could help me out here? In case it isn't possible to display SQL-Views, does anyone know of a better way to combine multiple tables and add a 'Location' column to trace the table from where the data originated? Any help would be greatly appreciated!</p>