asp.net-mvc 错误:无法在 LINQ to Entities 查询中构造实体或复杂类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17105377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:56:44  来源:igfitidea点击:

Error: The entity or complex type cannot be constructed in a LINQ to Entities query

asp.net-mvcasp.net-mvc-4

提问by macieira

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

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

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

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

My Controller:

我的控制器:

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

    }

My Model:

我的型号:

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?

任何人都可以帮助我吗?

回答by sTodorov

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

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

However, you can do a couple of things:

但是,您可以执行以下操作:

1) Select an anonymous type instead of entity like:

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) 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:

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) Use some DTO (Data transfer object) to project the properties that you want to select on to:

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