【问题标题】:Error: The entity or complex type cannot be constructed in a LINQ to Entities query错误:无法在 LINQ to Entities 查询中构造实体或复杂类型
【发布时间】:2013-06-14 09:31:46
【问题描述】:

我在使用 MVC 进行连接查询时遇到问题,我不知道为什么。

无法在 LINQ to Entities 查询中构造实体或复杂类型“Tusofona_Website.Models.site_noticias”。

我的控制器:

    private TusofonaDBs db = new TusofonaDBs();

    //
    // GET: /DestaquesMain/

    public ActionResult Index()
    {
        var query = (from sd in db.site_desquesnoticias
                    join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia
                    where sn.Destaque == 1
                    select new site_noticias {
                        CorpoNoticia = sn.CorpoNoticia,
                        TituloNoticia = sn.TituloNoticia
                    }).ToList();

        //return View(db.site_desquesnoticias.ToList());
          return View(query);

    }

我的模特:

public class site_destaquesnoticias
{
    [Key]
    public Int32 IDDestaque { get; set; }
    public Int32 IDNoticia { get; set; }
    public string Foto { get; set; }


}

public class site_noticias
{
    [Key]
    public Int32 IDNoticia { get; set; }
    public string CorpoNoticia { get; set; }
    public string TituloNoticia { get; set; }
    public string Foto { get; set; }
    public Int32 Destaque { get; set; }
}

public class TusofonaDBs : DbContext
{
    public DbSet<site_destaquesnoticias> site_desquesnoticias { get; set; }
    public DbSet<site_noticias> site_noticias { get; set; }
}

谁能帮帮我?

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-4


【解决方案1】:

您不能投影到映射实体上(请参阅this 答案)。

但是,您可以做几件事:

1) 选择匿名类型而不是实体,例如:

var query = (from sd in db.site_desquesnoticias
                    join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia
                    where sn.Destaque == 1
                    select new {
                        CorpoNoticia = sn.CorpoNoticia,
                        TituloNoticia = sn.TituloNoticia
                    }).ToList();

2) 反转查询以直接选择 site_noticias。这取决于您要检索的查询和数据。例如,您可以查看以下内容是否可行并为您提供所需的数据:

var query = (from sd in db.site_desquesnoticias
                    join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia
                    where sn.Destaque == 1
                    select sn).ToList();

3) 使用一些 DTO(数据传输对象)将您要选择的属性投影到:

   public class SiteNoticiasDTO{
     public string CorpoNoticia {get;set;}
     public string TituloNoticia {get;set;}
    }

var query = (from sd in db.site_desquesnoticias
                    join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia
                    where sn.Destaque == 1
                    select new SiteNoticiasDTO {
                        CorpoNoticia = sn.CorpoNoticia,
                        TituloNoticia = sn.TituloNoticia
                    }).ToList();

【讨论】:

  • 2)答案工作得很好。可能这是一个菜鸟错误,我仍然是 MVC 的新手 :)。非常感谢。
  • 这可行,但结果变得不可编辑。你如何保持结果可编辑?像 foreach(查询中的项目){ item.name = "append_something"; }。编辑项目不会返回任何错误,但它不起作用。
  • 3 对我不起作用,因为将 ProductDTO 转换为所需的产品实体返回错误:无法将类型“xyz.Controllers.ProductDTO”转换为类型“xyz.Models.Product”。 LINQ to Entities 仅支持转换 EDM 基元或枚举类型。我需要将 IQueriable 返回到分页方法:有没有办法从 DTO 转换回所需的实体?
  • 1.对我不起作用。与上面相同:转换为所需的返回类型我得到:'无法将类型匿名类型'转换为类型'xyz.Models.Product'。 LINQ to Entities 仅支持转换 EDM 基元或枚举类型
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-11
  • 2017-05-31
  • 2015-02-06
  • 1970-01-01
  • 2023-03-30
相关资源
最近更新 更多