2009-03-30 3 views
11

기본 CRUD 문을 사용하고 인터페이스를 사용하는 Entity Framework 리포지토리에 매우 일반적인 제네릭 리포지토리를 만들려고합니다. 나는 벽돌 벽 머리를 먼저 치고 넘어 뜨렸다. 다음은 Hurl이라는 테이블과 함께 Entity Framework Model을 사용하여 콘솔 응용 프로그램으로 작성된 코드입니다. 단순히 ID로 객체를 뒤로 당기려고합니다. 전체 응용 프로그램 코드는 다음과 같습니다. 나는이 정보를 추출하려고 시도하고 어디Entity Framework 일반 리포지토리 오류

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

이것은 : 여기

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

} 

내가 점점 오전 오류입니다.

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

+0


은 내가이 문제를 디버깅을 시작 갈 수있는 곳 짧은 대답은 것 같아요. –

답변

6

음,이 날 의아해했다. 나는 스티브 발터의 곧 나오는 ASP.NET MVC Unleashed 책에서 EFRepository의 일부를보고 난 후에 야생 찌르다가 작동하기 시작했다.이 방법을 바꾸면 문자열 형식의 차이를 알 수있다. 이것이 왜 이렇게 되는가에 대한 제안은 무엇입니까? 내가 이것을 보는 방식은 버그 일 수도 있습니다. 어쨌든 관심있는 사람들을 위해. (나는이 부분을 수정하는 것이 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

방금 ​​테스트를 위해 집에 도착했는데 이것을 추가 할 때까지 전체 솔루션이 작동하지 않았습니다. String.Format ("[{0} Set"] ​​(위의 해결 방법에서 적용 가능) –

+1

[{0} Set "]'과 같은 것을 하드 코딩하지 않고 이름을 얻으려면 다른 질문 인 http : : //stackoverflow.com/questions/3247288/error-in-generic-repository-method-for-entity-framework/3247456#3247456 – TheCloudlessSky

관련 문제