2009-03-30 21 views
11

Temel CRUD ifadelerine sahip ve bir Arabirim kullanan Entity Framework deposu için çok genel bir jenerik depo oluşturmaya çalışıyorum. Önce bir tuğla duvar kafasına çarptım ve devrildi. İşte bir konsol uygulamasında yazılan, bir Varlık Çerçeve Modeli kullanan ve Hurl isimli bir tablo içeren kodum. Sadece nesneyi kimliğine göre geri çekmeye çalışıyoruz. İşte tam uygulama kodu. Ben bu bilgileri ayıklamak çalışılıyor neredeVarlık Framework Genel Deposu Hatası

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

budur: İşte

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

} 

alıyorum hatadır.

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

+0


ben bu sorunu hata ayıklama başlatmak için gidebileceğini durumunda daha kısa bir cevap sanırım. –

cevap

6

Bu seferki beni şaşkın vardı. Ben vahşi bir bıçak aldı (Stephen Walther'ın gelecek ASP.NET MVC Unleashed kitabında EFRepository bir bölümünü gördükten sonra) ve çalışmaya başladı, işte düzeltme (Bu yöntemi değiştirin, dize biçimlendirmede fark dikkat edin). Bunun neden böyle olduğuyla ilgili herhangi bir öneriniz var mı? Gördüğüm şekilde, bir hata olabilir (ya da belki de yaptığım bir şey). İlgilenenler için herhangi bir oranda. (Bu kısmı düzeltmenin EFRepository @Keith Patton's blog post'un tüm işlevini düzelteceğini düşünürdüm).

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

Test etmek için eve geldim ve bu çözümü ekledikten sonra tam çözüm işe yaramadı. String.Format ("[{0} Set]", (Yukarıdaki çözüm uygulanabilir olduğunda) –

+1

"" [{0} Set "]' gibi bir kod yazmadan adı almak için, başka bir sorudaki gönderime bakın: http : //stackoverflow.com/questions/3247288/error-in-generic-repository-method-for-entity-framework/3247456#3247456 – TheCloudlessSky