2009-03-30 37 views
11

Tôi đang cố gắng tạo một kho lưu trữ generics rất chung chung cho kho lưu trữ khung Entity của tôi có các câu lệnh CRUD cơ bản và sử dụng một Giao diện. Tôi đã đánh đầu một bức tường gạch đầu tiên và bị đánh ngã. Đây là mã của tôi, được viết trong một ứng dụng giao diện điều khiển, sử dụng một Mô hình khung thực thể, với một bảng có tên là Hurl. Chỉ cần cố kéo đối tượng bằng ID của nó. Đây là mã ứng dụng đầy đủ.Entity Framework Generic Repository Error

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

} 

Đây là lỗi mà tôi nhận được:

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

Đây là nơi tôi đang cố gắng để lấy thông tin này từ đâu.

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

+0

Tôi đoán một câu trả lời ngắn là nơi tôi có thể đi để bắt đầu gỡ rối vấn đề này. –

Trả lời

6

Vâng, điều này khiến tôi bối rối. Tôi đã đâm một đoạn hoang dã (sau khi nhìn thấy một phần của EFRepository trong cuốn sách sắp ra mắt của ASP.NET Walther của ASP.NET Walther) và nó bắt đầu hoạt động, đây là bản sửa lỗi (Thay thế phương thức này, chú ý sự khác biệt trong định dạng chuỗi). Bất kỳ đề xuất nào về lý do tại sao đây là cách này? Cách mà tôi thấy điều này, nó có thể là một lỗi (hoặc có thể là điều tôi đang làm). Ở bất kỳ tỷ lệ nào cho bất kỳ ai quan tâm. (Tôi sẽ tưởng tượng sửa chữa phần này sẽ sửa toàn bộ chức năng của 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

Tôi vừa mới về nhà để kiểm tra và giải pháp đầy đủ không hoạt động cho đến SAU KHI tôi đã thêm phần này. String.Format ("[{0} Set]", (Nếu áp dụng trong giải pháp trên) –

+1

Để lấy tên mà không cần mã hóa một cái gì đó như '" [{0} Set "]', hãy xem bài đăng của tôi trên một câu hỏi khác: http : //stackoverflow.com/questions/3247288/error-in-generic-repository-method-for-entity-framework/3247456#3247456 – TheCloudlessSky