簡體   English   中英

實體框架通用存儲庫錯誤

[英]Entity Framework Generic Repository Error

我正在嘗試為我的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

好吧,這個讓我感到困惑。 我采取了狂野的刺(在Stephen Walther即將推出的ASP.NET MVC Unleashed書中看到EFRepository的一部分后)它開始工作,這里是修復(替換此方法,注意字符串格式的差異)。 有關為何這樣做的任何建議? 我看到這個的方式,可能是一個bug(或者我正在做的事情)。 無論如何,任何有興趣的人。 (我想想修復這部分將修復EFRepository @ Keith Patton的博客文章的整個功能)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM