Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架通用存储库错误_C#_Entity Framework_Generics_Repository Pattern - Fatal编程技术网

C# 实体框架通用存储库错误

C# 实体框架通用存储库错误,c#,entity-framework,generics,repository-pattern,C#,Entity Framework,Generics,Repository Pattern,我正在尝试为我的Entity Framework存储库创建一个非常通用的泛型存储库,该存储库包含基本CRUD语句并使用接口。我头先撞到了砖墙,被撞倒了。这是我在控制台应用程序中编写的代码,使用实体框架模型,表名为Hull。只需尝试通过其ID回调对象。以下是完整的应用程序代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Objects;

我正在尝试为我的Entity Framework存储库创建一个非常通用的泛型存储库,该存储库包含基本CRUD语句并使用接口。我头先撞到了砖墙,被撞倒了。这是我在控制台应用程序中编写的代码,使用实体框架模型,表名为Hull。只需尝试通过其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();
            }
        }
    }

}
这就是我试图从中提取这些信息的地方


嗯,这件事让我很困惑。我尝试了一下(在Stephen Walther即将出版的ASP.NET MVC发行版书中看到了eRepository的一部分后),它开始工作了,下面是修复方法(替换此方法,注意字符串格式的差异)。有没有关于为什么会这样的建议?在我看来,这可能是一个bug(或者我正在做的事情)。无论如何,对任何感兴趣的人来说。(我想修复这一部分将修复eForepository@的整个功能)

public IQueryable GetQuery()
{
类型baseType;
if(HasBaseType(类型为(T),输出baseType))
{
返回这个.ObjectContext.CreateQuery(String.Format(“[{0}]”,baseType.Name.ToString()).OfType();
}
其他的
{
返回此.ObjectContext.CreateQuery(String.Format(“[{0}]”,typeof(T.Name.ToString());
}
}

还有一个很好的例子:

我想一个简短的答案是我可以从哪里开始调试这个问题。我刚回到家进行测试,直到添加了这个问题,完整的解决方案才起作用。String.Format(“[{0}集]”,(在上述解决方案中适用的情况下)。要获得名称而不使用类似于
“[{0}集”]
的硬编码,请参阅我关于另一个问题的帖子:
    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."
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()));
    }
}