ADO.NET 实体框架按值范围关联实体
我有两个 EF 实体。其中一个有一个名为 HouseNumber 的属性。另一个有两个属性,一个称为 StartHouseNumber,一个称为 EndHouseNumber。
我想根据 HouseNumber 是否位于 StartHouseNumber 和 EndHouseNumber 之间,在这些元素之间创建多对多关联。但是,我似乎只能在 GUI 中获得等于运算符。
我是否缺少一种方法来通过范围来完成这种关联?
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber.
I want to create a many to many association between these elements based on whether or not HouseNumber falls between StartHouseNumber and EndHouseNumber. However, I can only seem to get an equals operator in the GUI.
Is there a way I'm missing to accomplish this association by range?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
理论上,您可以执行此操作,请参阅 这篇文章介绍了如何创建由视图支持的关联。
但正如其他人所说,这可能不是一个好主意。
亚历克斯
In theory you can do this see this post on creating an Association backed by a view.
But as the others have said it's probably not a good idea.
Alex
因此,在对这个主题进行了大量阅读后,答案是你不能这样做,而且这是一种不受欢迎的行为。
实体框架中的关联必须是读写的。如果我创建了所描述的关联,如果我将其中一个实体添加到另一个实体的关联集合中,会发生什么情况?它会为 HouseNumber 选择什么值以使其落在 Start 值和 End 值之间?
正确的方法是编写一个带有 HouseNumber 参数的辅助函数,并将其放置在第二个实体的手动编码部分类上。您还可以编写一个只读属性来进行查找或编写一个 getter 方法。或者,您可以在数据库中编写一个存储过程来执行此操作并将其导入实体中。
So after considerably more reading on this topic, the answer is that you can't do this, but also that it is an undesired behavior to have.
Associations in the entity framework must be read-write. If I create the association I described, what would happen if I added one of the entities to the other entity's association collection? What value would it choose for HouseNumber to make it fall between the Start and End values?
The correct way to do this is either to write a helper function that takes an argument for HouseNumber, and place this on a hand-coded partial class of the second entity. You'd also either write a read-only property to do the look up or a getter method. Alternatively, you could write a stored procedure in your database that can do this and import it into the entity.
据我所知,你不能通过关联来做到这一点,但你可以通过查询来做到这一点。
You can't do this with an association, as far as I know, but you can do it with a query.