实体框架通用存储库错误

时间:2009-03-30 02:57:56

标签: c# entity-framework generics repository-pattern

我正在尝试为我的Entity Framework存储库创建一个非常通用的泛型存储库,该存储库具有基本的CRUD语句并使用接口。我先打了一个砖墙头,然后被打倒了。这是我的代码,使用实体框架模型在控制台应用程序中编写,其中包含一个名为Hurl的表。只需尝试通过其ID撤回对象。这是完整的应用程序代码。

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

2 个答案:

答案 0 :(得分:7)

嗯,这个让我困惑的是。我采取了狂野的刺(在Stephen Walther即将推出的ASP.NET MVC Unleashed书中看到EFRepository的一部分后)它开始工作,这里是修复(替换此方法,注意字符串格式的差异)。有关为何这样做的任何建议?我看到这个的方式,可能是一个bug(或者我正在做的事情)。无论如何,任何有兴趣的人。 (我想这个部分会修复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()));
    }
}

答案 1 :(得分:0)

相关问题