Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq To Entites error: The entity or complex type .... cannot be constructed in a LINQ to Entities query
    primarykey
    data
    text
    <p>I have been pulling my hair out trying to do the MVC 3 Music Store tutorial found on the asp.net website. In the below code I am trying to use a Linq to Entities query to return results to my Browse view from the storeController but I receive this error when I navigate to the browse page: <em>The entity or complex type 'MvcMusicStore.Models.Genre' cannot be constructed in a LINQ to Entities query</em>. </p> <p>The code below works when I use the Lambda expression that they use in tutorial but I am more comfortable using a Linq Query. Please can someone explain to me why this doesn’t work using the following code? </p> <p><strong>Storecontoller.cs</strong></p> <pre><code>MusicStoreEntities storeDB = new MusicStoreEntities(); public ActionResult Browse(string genre) { //working code used in tutorial //var genreModel = storeDB.Genres.Include("Albums") //.Single(g =&gt; g.Name == genre); storeDB.Genres.Include("Albums"); var genreModel = from p in storeDB.Genres where p.Name == genre select new Genre { GenreId = p.GenreId, Name = p.Name, Description = p.Description, Albums = p.Albums }; return View(genreModel.Single()); } </code></pre> <p><strong>Genre.cs</strong></p> <pre><code>using System.Collections.Generic; namespace MvcMusicStore.Models { public class Genre { public int GenreId { get; set; } public string Name { get; set; } public string Description { get; set; } public List&lt;Album&gt; Albums { get; set; } } } </code></pre> <p><strong>MusicStoreEntities.cs</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; namespace MvcMusicStore.Models { public class MusicStoreEntities : DbContext { public DbSet&lt;Album&gt; Albums { get; set; } public DbSet&lt;Genre&gt; Genres { get; set; } } } </code></pre> <p><strong>browse.cshtml</strong></p> <pre><code>&lt;h2&gt;Browsing Genre: @Model.Name&lt;/h2&gt; &lt;ul&gt; @foreach (var album in Model.Albums) { &lt;li&gt; @album.Title &lt;/li&gt; } &lt;/ul&gt; </code></pre> <p>Many thanks desperate dave</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload