相关记录是否加载到HashSet或SortedSet中?
假设我们有实体框架 4 的 POCO 类:
public class Order
{
public long Id { get; set; }
public ISet<OrderItem> OrderItems { get; set; }
}
以及从数据库检索订单的方法:
public Order GetOrder(long orderId)
{
using (var context = new MyModelEntities())
{
return context.Orders.Include("OrderItems").Where(order => order.Id == orderId).FirstOrDefault();
}
}
假设我们这样做:
Order myOrder = GetOrder(1);
myOrder.OrderItems 是 HashSet 还是 SortedSet?有办法控制这个吗?
Assume we have POCO class for Entity Framework 4:
public class Order
{
public long Id { get; set; }
public ISet<OrderItem> OrderItems { get; set; }
}
And this method to retrieve the order from database:
public Order GetOrder(long orderId)
{
using (var context = new MyModelEntities())
{
return context.Orders.Include("OrderItems").Where(order => order.Id == orderId).FirstOrDefault();
}
}
So suppose we do this:
Order myOrder = GetOrder(1);
Is myOrder.OrderItems a HashSet or SortedSet? Is there a way to control this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好问题。
据我所知(据我所知,没有任何 MSDN/博客/文章可以消除/证明这一点),导航属性可以是任何类型,只要它实现
ICollection
。HashSet
和SortedSet
都实现了ICollection
,因此两者都是可行的候选者。您单步执行代码了吗?您应该能够看到哪个具体类得到了解决。
大多数人使用
ICollection
/IList
。为什么要将属性声明为ISet
?为什么不直接声明你想要的类型,而不是接口。
或者您可以尝试使用依赖项注入(
For().Use()
)。Good question.
As far as i know (and there is no MSDN/blog/article i am aware of that dispells/proves this), a navigational property can be of any type as long as it implements
ICollection<T>
.Both
HashSet<T>
andSortedSet<T>
implementICollection<T>
, so either would be viable candidates.Did you step through the code? You should be able to see which concrete class get's resolved.
Most people use
ICollection<T>
/IList<T>
. Why are you wanting to declare the property asISet<T>
?Why don't you just declare which type you want, instead of the interface.
Or you could try using dependency injection (
For<ISet>().Use<HashSet>()
).