title: Entity Framework Core with Custom Return Type - Cannot create a DbSet for 'BookInfo' because this type is not included in the model for the context
body: <p><strong>Entity Framework Core with Custom Return Type</strong></p>
  
  <p>I'm trying to query a Custom Return Type from Sql Server using EF Core 3.1. There are two entities Book and Author and I want to get the BookInfo that has BookId, BookName, AuthorName. Here's the following code. </p>
  
  <p>Stored Procedure:</p>
  
  <pre><code>CREATE PROC GetBookInfos
  AS
  SELECT Books.BookId, Books.Name AS BookName, Authors.FirstName+' '+Authors.LastName AS AuthorName
  FROM Books
  INNER JOIN Authors
  ON Books.AuthorId = Authors.AuthorId;
  </code></pre>
  
  <p><br/>Book.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  
  namespace EFCoreShop
  {
      public class Book
      {
          public long BookId { get; set; }
          public string Name { get; set; }
          public double Price { get; set; }
          public long AuthorId { get; set; }
          public Author Author { get; set; }
      }
  }
  </code></pre>
  
  <p>Author.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  
  namespace EFCoreShop
  {
      public class Author
      {
          public long AuthorId { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public List&lt;Book&gt; Books { get; set; }
      }
  }
  </code></pre>
  
  <p><br>BookInfo.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  using System.Text;
  
  namespace EFCoreShop
  {
      [NotMapped]
      public class BookInfo
      {
          public long BookId { get; set; }
          public string BookName { get; set; }
          public string AuthorName { get; set; }
      }
  }
  </code></pre>
  
  <p><br>ShopContext.cs</p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  using System;
  using System.Collections.Generic;
  using System.Text;
  
  namespace EFCoreShop
  {
      class ShopContext:DbContext
      {
  
          public DbSet&lt;Author&gt; Authors { get; set; }
          public DbSet&lt;Book&gt; Books { get; set; }
          public DbSet&lt;BookInfo&gt; BookInfos { get; set; }
  
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              optionsBuilder.UseSqlServer("Data Source=**********;Database=EFCoreShop;User ID=*********;Password=***********");
          }
  
  
      }
  }
  </code></pre>
  
  <p>Program.cs</p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace EFCoreShop
  {
      class Program
      {
          static void Main(string[] args)
          {
              using (ShopContext shopContext = new ShopContext())
              {
                  IEnumerable&lt;BookInfo&gt; bookInfos = shopContext.BookInfos.FromSqlRaw("exec GetBookInfos").ToList&lt;BookInfo&gt;();
  
                  foreach (var bookInfo in bookInfos)
                  {
                      Console.WriteLine($"Book Id: {bookInfo.BookId} Book Name: {bookInfo.BookName} Author Name: {bookInfo.AuthorName}");
                  } 
              }
          }
      }
  }
  </code></pre>
  
  <p>I think I have done everything good. I'm trying to return a custom type from the database and already have the store procedure in there. I don't want the entity to be mapped either. </p>
  
  <p>Note: I know I can do it using inner join in EF Core. But, I'm trying to get the data from stored procedure with custom return type. </p>
  
  <p>Could you guide me in fixing the issue.</p>
  
  <p>Error: </p>
  
  <pre><code>Unhandled exception. System.InvalidOperationException: Cannot create a DbSet for 'BookInfo' because this type is not included in the model for the context.
     at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
     at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.CheckState()
     at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
     at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
     at Microsoft.EntityFrameworkCore.RelationalQueryableExtensions.FromSqlRaw[TEntity](DbSet`1 source, String sql, Object[] parameters)
     at EFCoreShop.Program.Main(String[] args) in D:\Workspace\Learning\DotNet\EFCoreShop\EFCoreShop\Program.cs:line 14
  </code></pre>
  
  <p><strong>UPDATE:</strong> <br>
  This code below would solve the issue without using Stored Procedure. But, we want result using Stored Procedure. However, if you don't want to use Stored Procedure you can do this as well.</p>
  
  <p>Program.cs ( without store procedure, not what I want)</p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace EFCoreShop
  {
      class Program
      {
          static void Main(string[] args)
          {
              using (ShopContext shopContext = new ShopContext())
              {
                  //IEnumerable&lt;BookInfo&gt; bookInfos = shopContext.BookInfos.FromSqlRaw("exec GetBookInfos").ToList&lt;BookInfo&gt;();
  
                  //foreach (var bookInfo in bookInfos)
                  //{
                  //    Console.WriteLine($"Book Id: {bookInfo.BookId} Book Name: {bookInfo.BookName} Author Name: {bookInfo.AuthorName}");
                  //}
  
                  // This is more manageable than stored procedure
                  IEnumerable&lt;Book&gt; books = shopContext.Books.Include( book =&gt; book.Author ).ToList();
  
                  foreach (var book in books)
                  {
                      Console.WriteLine($"Book Id: {book.BookId} Book Name: {book.Name} Author Name: {book.Author.FirstName+' '+book.Author.LastName}");
                  }
  
              }
          }
      }
  }
  </code></pre>