기본 CRUD 문을 사용하고 인터페이스를 사용하는 Entity Framework 리포지토리에 매우 일반적인 제네릭 리포지토리를 만들려고합니다. 나는 벽돌 벽 머리를 먼저 치고 넘어 뜨렸다. 다음은 Hurl이라는 테이블과 함께 Entity Framework Model을 사용하여 콘솔 응용 프로그램으로 작성된 코드입니다. 단순히 ID로 객체를 뒤로 당기려고합니다. 전체 응용 프로그램 코드는 다음과 같습니다. 나는이 정보를 추출하려고 시도하고 어디Entity Framework 일반 리포지토리 오류
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."
이것은 : 여기
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();
}
}
}
}
내가 점점 오전 오류입니다.
http://blog.keithpatton.com/2008/05/29/Polymorphic+Repository+For+ADONet+Entity+Framework.aspx
은 내가이 문제를 디버깅을 시작 갈 수있는 곳 짧은 대답은 것 같아요. –