如何在实体框架中将实体的集合设为集合?
假设我正在 EF 中的空白画布上构建模型,并且模型中存在一对多关系(类别 -> 产品或其他关系)。如何使该集合(Category.Products)成为集合(HashSet 或类似的)而不是集合,以便我可以在模型级别强制执行集合约束(例如唯一性)?
Say I'm building a model from a blank canvas in EF and I have a one-to-many relationship in the model (Category->Product or something). How can I make that collection (Category.Products) a Set (HashSet or similar) instead of a collection, so that I can enforce set constraints (such as uniqueness) at the model level?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最近开始将 POCO 与 Linq-To-Sql 结合使用,并且非常喜欢它所提供的自由,不必使用 EntitySet 等。所以我认为 POCO 是你的答案,但我怀疑(还没有研究过,所以无法明确回答)你可以用于关联和框架(EF 或 L2S)的类型仍然会受到限制能够使用它们。例如,您可能必须使用从 IList 派生的东西或其他东西。
不久前,我正在研究一些隐约相似的东西,发现 EntitySet 的功能之一是能够订阅“添加”和“删除”事件。有一个 ObservableCollection 类型也具有类似的功能,因此您可以查看它们。否则,您很可能会陷入自己的困境。
I recently have moved to using POCO with Linq-To-Sql and really like the freedom it gives to not have to use EntitySet et al. So I think POCO is the answer for you, but I suspect (haven't researched it so can't answer definitively) there are going to be restrictions on what types you can use for your associations and the framework (EF or L2S) still be able to use them. For instance, you probably have to use something that derives from IList, or whatever.
I was looking at something vaguely similar a bit ago, and found that one of the features of EntitySet is the ability to subscribe to the Add and Remove events. There is an ObservableCollection type that also has similar functionality, so you can look into those. Otherwise, you're most probably stuck rolling your own.