2009-03-30 21 views
11

मैं अपने एंटिटी फ्रेमवर्क रिपोजिटरी के लिए एक बहुत सामान्य जेनेरिक रेपॉजिटरी बनाने की कोशिश कर रहा हूं जिसमें बुनियादी सीआरयूडी स्टेटमेंट हैं और इंटरफेस का उपयोग करता है। मैंने पहले एक ईंट की दीवार सिर मारा है और दस्तक दिया गया है। हर्ल नाम की एक तालिका के साथ, एंटीटी फ्रेमवर्क मॉडल का उपयोग करके कंसोल एप्लिकेशन में लिखा गया मेरा कोड यहां दिया गया है। बस वस्तु को अपनी आईडी से वापस खींचने की कोशिश कर रहा है। यहां पूर्ण आवेदन कोड है।इकाई फ्रेमवर्क जेनेरिक रिपोजिटरी त्रुटि

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

खैर, यह भी मुझे हैरान था। मैंने एक जंगली स्टैब लिया (स्टीफन वाल्थर की आगामी एएसपी.नेट एमवीसी अनलेश बुक में ईएफआरपोजिटरी का एक हिस्सा देखने के बाद) और यह काम करना शुरू कर दिया, यहां ठीक है (इस विधि को बदलें, स्ट्रिंग स्वरूपण में अंतर देखें)। इस तरह के सुझाव इस तरह क्यों हैं? जिस तरह से मैं इसे देखता हूं, यह एक बग हो सकता है (या हो सकता है कि मैं कुछ कर रहा था)। रुचि रखने वालों में से किसी के लिए किसी भी दर पर। (मुझे लगता है कि इस भाग को ठीक करने से 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

मैं अभी परीक्षण करने के लिए घर पहुंचा और पूर्ण समाधान तब तक काम नहीं किया जब तक मैंने इसे जोड़ा नहीं। स्ट्रिंग.फॉर्मैट ("[{0} सेट]", (उपर्युक्त समाधान में जहां लागू हो)। –

+1

'[{0} सेट "] जैसे हार्डकोडिंग के बिना नाम प्राप्त करने के लिए, मेरी पोस्ट को किसी अन्य प्रश्न पर देखें: http : //stackoverflow.com/questions/3247288/error-in-generic-repository-method-for-entity-framework/3247456#3247456 – TheCloudlessSky

संबंधित मुद्दे