EF4 and UnitOfWork - How to make custom properties work
Say I have:
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 N开发者_如何学GoEW 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();
}
}
And I'm working with Entity Framework (4.1 code first).
How would I implement for example a custom property in A class like:
//returns the total of all B objects in the context where the category is NEW
public int NewBTotals
{
}
And not having to create a context?
Hopefully my question is clear enough, if not please let me know and I'll try to be more descriptive of what I want to achieve (ran out of time).
I found that even by B not having a foreign key to A, I can create a navigation property from A to B and vice versa which kind of solves the problem on its own.
精彩评论