C# 实体框架通用存储库错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/695878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity Framework Generic Repository Error
提问by Jeff Ancel
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.
我正在尝试为我的 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();
}
}
}
}
Here is the error that I am getting:
这是我得到的错误:
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
http://blog.keithpatton.com/2008/05/29/Polymorphic+Repository+For+ADONet+Entity+Framework.aspx
采纳答案by Jeff Ancel
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).
嗯,这个让我很困惑。我大胆尝试(在 Stephen Walther 即将出版的 ASP.NET MVC Unleashed 书中看到了 EFRepository 的一部分之后),它开始工作,这是修复(替换此方法,注意字符串格式的差异)。关于为什么会这样的任何建议?在我看来,这可能是一个错误(或者我正在做的事情)。无论如何,对于任何有兴趣的人来说。(我想修复这部分将修复 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()));
}
}