2009-03-30 18 views
11

Sto cercando di creare un repository generico molto generico per il mio repository di Entity Framework che ha le istruzioni CRUD di base e utilizza un'interfaccia. Ho colpito per primo un muro di mattoni e sono stato rovesciato. Ecco il mio codice, scritto in un'applicazione console, utilizzando un modello di Entity Framework, con una tabella denominata Hurl. Semplicemente provando a ritrarre l'oggetto tramite il suo ID. Ecco il codice completo dell'applicazione.Errore di repository generico di Entity Framework

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data.Objects; 
using System.Linq.Expressions; 
using System.Reflection; 
using System.Data.Objects.DataClasses; 

namespace GenericsPlay 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var hs = new HurlRepository(new hurladminEntity()); 
      var hurl = hs.Load<Hurl>(h => h.Id == 1); 
      Console.Write(hurl.ShortUrl); 
      Console.ReadLine(); 

     } 
    } 

    public interface IHurlRepository 
    { 
     T Load<T>(Expression<Func<T, bool>> expression); 
    } 

    public class HurlRepository : IHurlRepository, IDisposable 
    { 

     private ObjectContext _objectContext; 

     public HurlRepository(ObjectContext objectContext) 
     { 
      _objectContext = objectContext; 
     } 

     public ObjectContext ObjectContext 
     { 
      get 
      { 
       return _objectContext; 
      } 
     } 

     private Type GetBaseType(Type type) 
     { 
      Type baseType = type.BaseType; 
      if (baseType != null && baseType != typeof(EntityObject)) 
      { 
       return GetBaseType(type.BaseType); 
      } 
      return type; 
     } 

     private bool HasBaseType(Type type, out Type baseType) 
     { 
      Type originalType = type.GetType(); 
      baseType = GetBaseType(type); 
      return baseType != originalType; 
     } 

     public IQueryable<T> GetQuery<T>() 
     { 
      Type baseType; 
      if (HasBaseType(typeof(T), out baseType)) 
      { 
       return this.ObjectContext.CreateQuery<T>("[" + baseType.Name.ToString() + "]").OfType<T>(); 
      } 
      else 
      { 
       return this.ObjectContext.CreateQuery<T>("[" + typeof(T).Name.ToString() + "]"); 
      } 
     } 

     public T Load<T>(Expression<Func<T, bool>> whereCondition) 
     { 
      return this.GetQuery<T>().Where(whereCondition).First(); 
     } 

     public void Dispose() 
     { 
      if (_objectContext != null) 
      { 
       _objectContext.Dispose(); 
      } 
     } 
    } 

} 

Ecco l'errore che sto ottenendo:

System.Data.EntitySqlException was unhandled 
    Message="'Hurl' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly., near escaped identifier, line 3, column 1." 
    Source="System.Data.Entity" 
    Column=1 
    ErrorContext="escaped identifier" 
    ErrorDescription="'Hurl' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly." 

Questo è dove sto cercando di estrarre queste informazioni da.

http://blog.keithpatton.com/2008/05/29/Polymorphic+Repository+For+ADONet+Entity+Framework.aspx

+0

immagino una risposta più breve è dove potrei andare per iniziare il debug questo problema. –

risposta

6

Beh, questo mi aveva perplesso. Ho preso una pugnalata selvaggia (dopo aver visto una parte dell'EFRepository nel prossimo libro di ASP.NET MVC Unleashed di Stephen Walther) e ha iniziato a funzionare, ecco la correzione (Sostituisci questo metodo, nota la differenza nella formattazione della stringa). Qualche suggerimento sul perché questo è così? Il modo in cui vedo questo, potrebbe essere un bug (o forse qualcosa che stavo facendo). In ogni caso per qualcuno di quelli interessati. (Immagino che riparare questa parte risolverà l'intera funzione dell'EFR-repository @Keith Patton's blog post).

public IQueryable<T> GetQuery<T>() 
{ 
    Type baseType; 
    if (HasBaseType(typeof(T), out baseType)) 
    { 
     return this.ObjectContext.CreateQuery<T>(String.Format("[{0}]", baseType.Name.ToString())).OfType<T>(); 
    } 
    else 
    { 
     return this.ObjectContext.CreateQuery<T>(String.Format("[{0}]", typeof(T).Name.ToString())); 
    } 
} 
+0

Sono appena arrivato a casa per testare e la soluzione completa non ha funzionato fino a quando non ho aggiunto questo. String.Format ("[{0} Set]", (Dove applicabile nella soluzione di cui sopra). –

+1

Per ottenere il nome senza hardcoding qualcosa come '" [{0} Set "]', vedere il mio post su un'altra domanda: http : //stackoverflow.com/questions/3247288/error-in-generic-repository-method-for-entity-framework/3247456#3247456 – TheCloudlessSky

Problemi correlati