성능 문제가있는 SQL Server에서 Entity Framework를 사용하는 프로젝트 ASP.NET MVC3에서 작업하고 있습니다.Entity Framework - 쿼리 실행 성능 문제
EF 및 Linq를 사용하여 뷰/테이블에서 데이터를로드 할 때마다. SQL Server Profiler에서는 모든 조건이 프로파일 러에 나타나지 않기 때문에 모든 테이블/뷰 내용을 검색 할 수 있습니다. 나중에 LINQ에 의해 필터링됩니다 생각합니다.
맞습니까? SQL Server에서 처음에 필요한 데이터 만로드하는 방법?
var query = unitWork.City.GetFirstorDefault(item => item.City == cityCode);
데이터 호출 예를 들어 우리의 데이터 영역의 추출 다음은
이 내 예제 코드입니다. 답장을 보내 주셔서 감사합니다. 여기
public class UnitOfWork : IUnitOfWork, IDisposable
{
#region CONSTRUCTOR
public UnitOfWork()
{
_context = new MyApplicationEntities(); //DataContext
_context.ContextOptions.LazyLoadingEnabled = true;
_context.ContextOptions.UseLegacyPreserveChangesBehavior = false;
}
#endregion
// DESC_RECHARGEABLE is a table in DB
public IGenericRepository<DESC_RECHARGEABLE> RepRechargeable
{
get{return _repRechargeable ?? (_repRechargeable = new GenericRepository<DESC_RECHARGEABLE>(_context));}
}
}
public interface IGenericRepository<T> : ICollection<T>
where T : class
{
IEnumerable<T> Query(Func<T, bool> predicate);
void Update(T entity);
T GetFirstorDefault(Func<T, bool> predicate);
IEnumerable<T> GetAll();
T GetByKey(Func<T, bool> predicate);
bool Remove(T entity);
void Add(T entity);
ObjectSet<T> GetQuery();
}
public class GenericRepository<T> : IGenericRepository<T>
where T : class
{
private MyApplicationEntities Currentcontext;
public ObjectSet<T> entitySet;
private List<GenericRepository<T>> _list = null;
private string entityName;
public GenericRepository(MyApplicationEntities context)
{
if (context == null)
throw new ArgumentNullException("context");
this.Currentcontext = context;
this.entitySet = context.CreateObjectSet<T>();
this.entityName = entitySet.Name;
}
#region READ
public IEnumerable<T> Query(Func<T, bool> predicate)
{
return this.entitySet.Where(predicate);
}
public T GetFirstorDefault(Func<T, bool> predicate)
{
return this.Query(predicate).FirstOrDefault();
}
public IEnumerable<T> GetAll()
{
return this.entitySet.AsEnumerable();
}
public T GetByKey(Func<T, bool> predicate)
{
return this.Query(predicate).FirstOrDefault();
}
#endregion
}
// 클라이언트 호출 예를 들어, 대부분의 가능성이 unitOfWork.City
또는 .GetFirstOrDefault
방법 구현이 DbSet<T>
전에 필터를 적용에 열거 된 조건
var tempList = _unitofWork.RepRechargeable.Query(item => item.COMPANY_CODE == companyCode
&& item.DIVISION_CODE == divisionCode && !string.IsNullOrEmpty(item.PROPERTY));
당신은 우리에게 당신의 Linq에 표시되어야를 code –
코드 –
은'unitWork.City'와'GetFirstorDefault' 메쏘드의 구현을 보여줍니다. – Moho