实体框架的通用仓库错误 [英] Entity Framework Generic Repository Error

查看:165
本文介绍了实体框架的通用仓库错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建我的实体框架库中一个非常通用的泛型库具有基本的CRUD语句和使用接口。我已经先撞了南墙头被打翻了。这里是我的代码,写在一个控制台应用程序,使用实体框架模型,一个叫投表。只是想通过它的ID给拉了回来的对象。这里是完整的应用程序代码

I am trying to create a very generic generics repository for my Entity Framework repository that has the basic CRUD statements and uses an Interface. I have hit a brick wall head first and been knocked over. Here is my code, written in a console application, using a Entity Framework Model, with a table named Hurl. Simply trying to pull back the object by its ID. Here is the full application code.

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

这就是我试图提取这些信息。

This is where I am attempting to extract this information from.

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

推荐答案

那么,这一次让我不解。我花了野生刺(看到了斯蒂芬·瓦尔特公司即将推出的ASP.NET MVC偷跑书的EFRepository的部分后),它开始工作,这里是修复(替换此方法,注意在字符串格式化的差异)。任何建议,这是为什么这样?我看到这个问题的方法,它可能是一个错误(或者也可以是我做的)。无论如何,任何有兴趣的。 (我想象修复此部分将解决EFRepository @的基斯巴顿的博客帖子)。

Well, this one had me puzzled. I took a wild stab (after seeing a portion of the EFRepository in Stephen Walther's upcoming ASP.NET MVC Unleashed book) and it started working, here is the fix (Replace this method, notice the difference in the string formatting). Any suggestions as to why this is this way? The way that I see this, it may be a bug (or maybe something I was doing). At any rate for any of those interested. (I would imagine fixing this portion will fix the entire function of the 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()));
    }
}

这篇关于实体框架的通用仓库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆