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 代码)。
例如,我将如何在 A 类中实现自定义属性,例如:
//returns the total of all B objects in the context where the category is NEW
public int NewBTotals
{
}
并且不必创建上下文?
希望我的问题足够清楚,如果不是,请告诉我,我会尝试更详细地描述我想要实现的目标(时间不够了)。
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 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();
}
}
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现即使 B 没有 A 的外键,我也可以创建从 A 到 B 的导航属性,反之亦然,这可以自行解决问题。
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.