Linq To Entites 错误:实体或复杂类型...无法在 LINQ to Entities 查询中构造

发布于 2024-10-31 01:50:04 字数 2100 浏览 0 评论 0原文

我一直在绞尽脑汁地尝试完成在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2024-11-07 01:50:04

您必须使用此:

var genreModel = from p in storeDB.Genres.Include("Albums")
                 where p.Name == genre
                 select p;
return View(genreModel.Single());

Include 必须是查询的一部分。提前打电话是不行的。您可以这样做:

var query = storeDB.Genres.Include("Albums");
var genreModel = from p in query
                 where p.Name == genre
                 select p;

您也无法创建到实体类(new Genre)的投影。您必须直接选择它。

You must use this:

var genreModel = from p in storeDB.Genres.Include("Albums")
                 where p.Name == genre
                 select p;
return View(genreModel.Single());

Include must be part of query. Calling it in advance doesn't work. You can do this:

var query = storeDB.Genres.Include("Albums");
var genreModel = from p in query
                 where p.Name == genre
                 select p;

Also you can't create projection to entity class (new Genre). You must select it directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文