2009-03-30 28 views
11

基本的なCRUDステートメントを持ち、インターフェイスを使用するEntity Frameworkリポジトリ用の非常に汎用的なジェネリックリポジトリを作成しようとしています。私は最初にレンガの壁の頭を打って、ノックされている。ここでは、Hurlという名前のテーブルを使用して、Entity Framework Modelを使用して、コンソールアプリケーションで記述された自分のコードを示します。 IDでオブジェクトを取り戻そうとするだけです。ここに完全なアプリケーションコードがあります。ここで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(); 
      } 
     } 
    } 

} 

は、私が取得していますエラーです:

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

私がこの情報を抽出しようとしています場所です。

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

+0

私はこの問題のデバッグを開始するために行くかもしれない場所を短く答えがあると思います。 –

答えて

6

まあ、これは私が困惑していました。私は野生の刺し傷(Stephen Waltherの今後の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

私はテストのために家に着いたばかりで、これを追加するまで完全な解決策は機能しませんでした。 –

+1

[{0} Set "]'のようなものをハードコーディングせずに名前を取得するには、別の質問の投稿を参照してください:http( "[{0} Set" ://stackoverflow.com/questions/3247288/error-in-generic-repository-method-for-entity-framework/3247456#3247456 – TheCloudlessSky