2013-06-14 40 views
6

Tôi có một vấn đề với tham gia truy vấn với MVC và tôi không biết tại sao.Lỗi: thực thể hoặc loại phức tạp không thể được xây dựng trong một truy vấn LINQ to Entities

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

điều khiển của tôi:

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

    } 

mẫu của tôi:

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

Bất cứ ai có thể giúp tôi?

+0

Bản sao có thể có của [Thực thể không thể được tạo trong truy vấn LINQ to Entities] (http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a- LINQ-to-entity-query) –

Trả lời

14

Bạn không thể chiếu lên một thực thể ánh xạ (xem this câu trả lời).

Tuy nhiên, bạn có thể làm một vài điều:

1) Chọn một loại vô danh thay vì tổ chức như:

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) Đảo ngược truy vấn của bạn để chọn site_noticias trực tiếp. Điều đó phụ thuộc vào truy vấn và dữ liệu mà bạn muốn truy xuất. Ví dụ, bạn có thể có một cái nhìn nếu sau đây sẽ làm việc và cung cấp cho bạn các dữ liệu mà bạn cần:

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) Sử dụng một số DTO (đối tượng chuyển giao dữ liệu) để dự kiến ​​tính năng mà bạn muốn chọn vào :

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

công việc 2) câu trả lời tốt. Có lẽ đây là một lỗi noob, im vẫn còn mới với MVC :). Cảm ơn bạn rất nhiều. – macieira

+0

này hoạt động, nhưng kết quả trở nên không thể chỉnh sửa. Làm thế nào để bạn giữ kết quả có thể chỉnh sửa? Giống như foreach (mục trong truy vấn) {item.name = "append_something"; }. Chỉnh sửa các mục không trả lại bất kỳ lỗi nào, nhưng nó không hoạt động. – doncadavona

+0

3 không hoạt động đối với tôi khi truyền ProductDTO đến cần thiết Lỗi trả về thực thể sản phẩm: Không thể truyền loại 'xyz.Controllers.ProductDTO' để nhập 'xyz.Models.Product'. LINQ to Entities chỉ hỗ trợ đúc các kiểu nguyên thủy hoặc liệt kê EDM. Tôi cần phải trả lại IQueriable phương pháp phân trang: là có một cách để đúc lại từ DTO để thực thể yêu cầu? – Luke

Các vấn đề liên quan