Linq To Entites 错误:实体或复杂类型...无法在 LINQ to Entities 查询中构造
我一直在绞尽脑汁地尝试完成在 asp.net 网站上找到的 MVC 3 Music Store 教程。 在下面的代码中,我尝试使用 Linq to Entities 查询将结果从 storeController 返回到我的浏览视图,但当我导航到浏览页面时收到此错误: 无法在 LINQ to Entities 查询中构造实体或复杂类型“MvcMusicStore.Models.Genre”。
当我使用教程中使用的 Lambda 表达式时,下面的代码有效,但我更喜欢使用 Linq 查询。请有人向我解释为什么使用以下代码不起作用?
Storecontoller.cs
MusicStoreEntities storeDB = new MusicStoreEntities();
public ActionResult Browse(string genre)
{
//working code used in tutorial
//var genreModel = storeDB.Genres.Include("Albums")
//.Single(g => 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());
}
Genre.cs
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<Album> Albums { get; set; }
}
}
MusicStoreEntities.cs
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<Album> Albums { get; set; }
public DbSet<Genre> Genres { get; set; }
}
}
browse.cshtml
<h2>Browsing Genre: @Model.Name</h2>
<ul>
@foreach (var album in Model.Albums)
{
<li>
@album.Title
</li>
}
</ul>
非常感谢 绝望的戴夫
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:
The entity or complex type 'MvcMusicStore.Models.Genre' cannot be constructed in a LINQ to Entities query.
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?
Storecontoller.cs
MusicStoreEntities storeDB = new MusicStoreEntities();
public ActionResult Browse(string genre)
{
//working code used in tutorial
//var genreModel = storeDB.Genres.Include("Albums")
//.Single(g => 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());
}
Genre.cs
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<Album> Albums { get; set; }
}
}
MusicStoreEntities.cs
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<Album> Albums { get; set; }
public DbSet<Genre> Genres { get; set; }
}
}
browse.cshtml
<h2>Browsing Genre: @Model.Name</h2>
<ul>
@foreach (var album in Model.Albums)
{
<li>
@album.Title
</li>
}
</ul>
Many thanks
desperate dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用此:
Include
必须是查询的一部分。提前打电话是不行的。您可以这样做:您也无法创建到实体类(
new Genre
)的投影。您必须直接选择它。You must use this:
Include
must be part of query. Calling it in advance doesn't work. You can do this:Also you can't create projection to entity class (
new Genre
). You must select it directly.