Databinding ASP.net DropDownList con Entity Framework
-
03-07-2019 - |
Domanda
Sto cercando di associare un DropDownList ASP.net ai risultati di una query del framework di entità, mantenendo comunque la separazione multi-livello. (ovvero non voglio che il mio codice UI contenga i dettagli della query, né il mio codice Livello dati abbia dipendenze UI.) Il mio codice nel gestore dell'evento Page_Load è simile al seguente:
IEnumerable<Lookup> TypesLookup = Business.DocumentBO.GetDocumentTypes(_LookupTypeID);
DocTypeDropDownList.DataSource = TypesLookup;
DocTypeDropDownList.DataTextField = "Description";
DocTypeDropDownList.DataValueField = "LookupID";
DocTypeDropDownList.DataBind();
Anche se il mio codice dati è simile a questo (esiste anche un livello aziendale intermedio, ma non vi è ancora elaborazione lì - solo un pass-through.):
public static IEnumerable<Lookup> GetLookups(int LookupTypeID)
{
using (VLFDocumentEntities context = new VLFDocumentEntities())
{
IEnumerable<Lookup> l = (from c in context.Lookup
where c.LookupTypeID == LookupTypeID
select c);
return l;
}
}
Quando arrivo a DocTypeDropDownList.DataBind () ;, genera un ObjectDisposedException con il messaggio " DocTypeDropDownList.DataBind (); " ;. Qualcuno può consigliarmi il modo migliore per affrontare questo?
Grazie, Andy
Soluzione
Non devi staccare gli oggetti dal contesto? Per esempio:
IEnumerable<Lookup> l = (from c in context.Lookup
where c.LookupTypeID == LookupTypeID
select c);
foreach (Lookup lookup in l)
context.Detach(lookup);
return l;
Altri suggerimenti
Perché non usi semplicemente un Elenco < > ;?
public static List<Lookup> GetLookups(int LookupTypeID)
{
using (VLFDocumentEntities context = new VLFDocumentEntities())
{
return (from c in context.Lookup
where c.LookupTypeID == LookupTypeID
select c).ToList();
}
}