我有存储库来管理ComplaintTypes
上的操作,它有大量的关联实体,所以我定义了。不想加载所有内容,因此我有LazyLoadingEnabled = true;
。然而,我确实想要加载一个关联的实体,例如:ComplaintSubType
只加载一个关联的实体
这对我有用,但我在想有更好的方法吗?谢谢!
namespace Complaint.Dal.Repositories
{
public class ComplaintTypeRepository : RepositoryBase<ComplaintType>, IComplaintTypeRepository
{
#region ctor
/// <summary>
/// ctor
/// </summary>
/// <param name="objectContext"></param>
public ComplaintTypeRepository(IObjectContext objectContext)
: base(objectContext)
{
//Lazy Load so we don't get bloated data
objectContext.LazyLoadingEnabled = true;
}
#endregion
#region Implementation of IComplaintRepository
public IEnumerable<ComplaintType> GetAllComplaintTypes()
{
//Load the related SubTypes
var result = GetAll(t => t.PK_Type_Id);
foreach (var complaintType in result)
{
complaintType.ComplaintSubType.Load();
}
return result;
}
public ComplaintType GetComplaintType(int typeId)
{
var result = GetSingle(t => t.PK_Type_Id == typeId);
result.ComplaintSubType.Load();
return result;
}
public void UpdateComplaintType(ComplaintType entity)
{
Attach(entity);
}
#endregion
}
}
更新时间:
public IEnumerable<T> GetAll<TKey, TType>(Expression<Func<T, TKey>> orderBy)
{
var ret = ObjectSet;
//set Orderby
ret.OrderBy(orderBy);
return ret.ToList();
}
对不起,忘了提,我使用的是n层拱。所以这个实体没有附加。 – MisterIsaak 2011-05-26 20:32:31
好的,那么为了完全理解要告诉你什么,我需要看到更多的基础架构,GetAll和GetSingle方法...... – 2011-05-26 23:28:53