0
說我有:EF4和的UnitOfWork - 如何讓自定義屬性工作
public class A
{
public virtual int Id { get; set; }
}
public class ARepository
{
private SomeContext _context;
public ARepository(IUnitOfWork unitOfWork)
{
_context = unitOfWork;
}
public A GetAById(int aId)
{
return _context.A.Where(o => o.Id == aId).SingleOrDefault();
}
}
public class B
{
public virtual int Id { get; set; }
public virtual Category Category { get ; set; } //enum with NEW and OLD values
public virtual int Quantity { get; set; }
}
public class BRepository
{
private SomeContext _context;
public BRepository(IUnitOfWork unitOfWork)
{
_context = unitOfWork;
}
public B GetBById(int bId)
{
return _context.B.Where(o => o.Id == bId).SingleOrDefault();
}
}
而且我與實體框架(第4.1代碼)工作。
我將如何實現例如在類如自定義屬性:
//returns the total of all B objects in the context where the category is NEW
public int NewBTotals
{
}
而不必創建上下文?
希望我的問題已經夠清楚了,如果不是,請讓我知道,我會試着更加描述我想達到的目標(耗盡時間)。
你的代碼似乎有點不完整,就像你的'Category'屬性缺少它的類型或名稱。而且我不確定是否有'A'和'B'之間的關係,但是我會假設'A'具有與B的集合或關係。在任何一種情況下,我都會建議'GetNewBTotals'實現爲擴展方法而不是自定義屬性(該名稱實際上意味着它是一種方法)。 – CodingGorilla
基本上,由於我無法修改的錯誤的數據庫設計,A通過名爲「Source_reference」的列與B有關係。然而,B也可以引用X,Y或Z.爲了找出它依賴的父類,我們應該查找「源」列。如果我使用擴展方法,我必須以某種方式傳遞上下文,這不知何故我不確定什麼是最好的方法。 –
你想總結些什麼?您的評論說「上下文中的所有B對象的總數*」。但是,你必須有一個背景,爲什麼你不想創建一個?聽起來很奇怪,因爲上下文中的內容取決於之前做過的查詢的類型或附加的內容等。 – Slauma