2013-06-14 9 views
6

में नहीं किया जा सकता है मुझे एमवीसी के साथ क्वेरी में शामिल होने में कोई समस्या है और मुझे नहीं पता कि क्यों।त्रुटि: इकाई या जटिल प्रकार का निर्माण LINQ से Entities क्वेरी

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

किसी को भी मेरी मदद कर सकते?

+0

संभावित डुप्लिकेट [इकाई संस्थाओं के लिए एक LINQ का निर्माण नहीं किया जा सकता क्वेरी] (http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a- linq-to-entities-query) –

उत्तर

14

आप मैप किए गए इकाई पर प्रोजेक्ट नहीं कर सकते (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) गुण है कि आप करने के लिए पर चयन करना चाहते परियोजना के लिए कुछ डीटीओ (डेटा स्थानांतरण वस्तु) का उपयोग करें :

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

2) उत्तर काम ठीक है। शायद यह एक नोब त्रुटि है, मैं अभी भी एमवीसी के साथ नया हूँ :)। बहुत बहुत धन्यवाद। – macieira

+0

यह काम करता है, लेकिन परिणाम uneditable हो जाता है। आप परिणाम संपादन योग्य कैसे रखते हैं? Foreach (क्वेरी में आइटम) की तरह {item.name = "append_something"; }। वस्तुओं को संपादित करने से कोई त्रुटि नहीं आती है, लेकिन यह काम नहीं करती है। – doncadavona

+0

3 उत्पाद उत्पाद टीटीओ को आवश्यक उत्पाद इकाई रिटर्न त्रुटि के रूप में मेरे लिए काम नहीं किया: 'xyz.Controllers.ProductDTO' टाइप करने में असमर्थ 'xyz.Models.Product' टाइप करने में असमर्थ। LINQ से संस्थाएं केवल ईडीएम आदिम या गणना प्रकार कास्टिंग का समर्थन करती हैं। मुझे पेजिंग विधि में IQueriable वापस करने की आवश्यकता है: क्या डीटीओ से आवश्यक इकाई में वापस जाने का कोई तरीका है? – Luke

संबंधित मुद्दे