2009-03-30 16 views
11

Próbuję utworzyć bardzo ogólne repozytorium generyczne dla mojego repozytorium Entity Framework, które ma podstawowe instrukcje CRUD i używa interfejsu. Najpierw uderzyłem w ścianę z cegły i zostałem przewrócony. Oto mój kod, napisany w aplikacji konsolowej, za pomocą Entity Framework Model, z tabelą o nazwie Hurl. Po prostu próbuję odciągnąć obiekt według jego identyfikatora. Oto pełny kod aplikacji.Błąd Entity Framework Generic Repository

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

} 

Tutaj jest błąd, że jestem coraz:

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." 

To gdzie ja próbując wyodrębnić te informacje z.

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

+0

Chyba krótsza odpowiedź brzmi, gdzie mogę iść, aby rozpocząć debugowanie ten problem. –

Odpowiedz

6

No to trzeba było mnie zdziwiony. Wziąłem dzikie ukłucie (po zobaczeniu fragmentu EFRepository w nadchodzącej książce Stephena Walthera ASP.NET MVC Unleashed) i zaczęło działać, oto poprawka (Zastąp tę metodę, zauważ różnicę w formatowaniu ciągów znaków). Wszelkie sugestie, dlaczego tak się dzieje? Sposób, w jaki to widzę, może być błędem (lub może czymś, co robiłem). W każdym razie dla każdego zainteresowanego. (Wyobrażam sobie, że naprawienie tej części naprawi całą funkcję EFRepository @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

Właśnie przyjechałem do domu, aby przetestować i pełne rozwiązanie nie działało, dopóki PO tego nie dodałem. String.Format ("[{0} Set]", (Jeśli dotyczy powyższego rozwiązania) –

+1

Aby uzyskać nazwę bez kodowania fizycznego, np. "[{0} Ustaw"] ', zobacz mój wpis na inne pytanie: http : //stackoverflow.com/questions/3247288/error-in-generic-repository-method-for-entity-framework/3247456#3247456 – TheCloudlessSky