Entity Framework Generische Repository-Fehler
Ich versuche, um eine sehr generische Generika repository für meine Entity Framework-repository, hat die grundlegenden CRUD-Anweisungen und verwendet eine Schnittstelle. Ich habe gegen eine Wand geprallt Kopf erste und wurde umgeworfen. Hier ist mein code, geschrieben in einer Konsole-Anwendung, die mit einem Entity Framework-Modell mit einer Tabelle mit dem Namen Schleudern. Einfach versuchen nach hinten zu ziehen, das Objekt anhand seiner ID. Hier ist der vollständige code der Anwendung.
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();
}
}
}
}
Hier ist die Fehlermeldung, die ich erhalte:
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."
Dies ist, wo ich bin versucht, diese Informationen zu extrahieren.
http://blog.keithpatton.com/2008/05/29/Polymorphic+Repository+Für+ADONet+Einheit+Rahmen.aspx
InformationsquelleAutor Jeff Ancel | 2009-03-30
Du musst angemeldet sein, um einen Kommentar abzugeben.
Gut, das hatte mich verwirrt. Ich nahm einen wilden stab (nachdem ich einen Teil der EFRepository in Stephen Walther kommenden ASP.NET MVC-Entfesselt-Buch) und es begann zu arbeiten, hier ist das Update (Ersetzen Sie diese Methode, den Unterschied in der string-Formatierung). Irgendwelche Vorschläge, warum dies so ist? Die Art und Weise, dass ich dies sehen, kann es ein bug (oder vielleicht auch etwas, was ich tun). Jedenfalls für alle interessierten. (Ich könnte mir vorstellen, die Festsetzung dieser Teil wird fix die gesamte Funktion des EFRepository @ Keith Patton ' s blog-post).
Um den Namen ohne hardcoding so etwas wie
"[{0}Set"]
siehe mein post auf eine andere Frage: stackoverflow.com/questions/3247288/...InformationsquelleAutor Jeff Ancel
Hier ist noch ein guter:
http://blog.zoolutions.se/post/2010/04/05/Generic-Repository-for-Entity-Framework-for-Pluralized-Entity-Set.aspx
InformationsquelleAutor Shimmy