错误:实体或复杂类型不能在一个LINQ被构造为实体的查询 [英] Error: The entity or complex type cannot be constructed in a LINQ to Entities query

查看:265
本文介绍了错误:实体或复杂类型不能在一个LINQ被构造为实体的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,连接查询与MVC,我不知道为什么。

I have a problem with join query with MVC and i dont know why.

实体或复杂类型'Tusofona_Website.Models.site_noticias'不能在一个LINQ被构造为实体的查询。

The entity or complex type 'Tusofona_Website.Models.site_noticias' cannot be constructed in a LINQ to Entities query.

我的控制器:

    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; }
}

任何人都可以帮我吗?

Anyone can help me?

推荐答案

您不能投射到一个映射实体(见<一href=\"http://stackoverflow.com/questions/12916080/the-entity-or-complex-type-cannot-be-constructed-in-a-linq-to-entities-query\">this答案)。

You can't project onto a mapped entity (see this answer).

不过,你可以做两件事情:

However, you can do a couple of things:

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

1) Select an anonymous type instead of entity like:

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。这取决于查询和您想检索数据。例如,你可以去看看,如果下面的工作,给你,你需要的数据:

2) Invert your query to select the site_noticias directly. That depends on the query and the data that you would like to retrieve. For example, you can have a look if the following will work and give you the data that you need:

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(数据传输对象)项目要选择的属性:

3) Use some DTO (Data transfer object) to project the properties that you want to select on to:

   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();

这篇关于错误:实体或复杂类型不能在一个LINQ被构造为实体的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆