如何在没有单独实体的情况下映射多对多查找值?
假设我有一个包含国家/地区集合的实体组织
。除了用组织数据显示国家/地区(可能用于过滤)之外,我并不真正关心国家/地区,仅此而已。在我的对象模型中,我更喜欢使用纯 string
来表示国家/地区:
class Organization
{
...
public virtual IList<string> Countries { get; set; }
...
}
这很容易映射为一对多,使用仅包含国家/地区的组件集合名称元素。这将产生如下表:
OrganizationCountries
-----------------------
Organization_id int
CountryName string
但是为了避免大量冗余并能够轻松更新国家/地区名称,我想将国家/地区名称保留在单独的表中(如查找表),以便在数据库中我有:
OrganizationCountries
-----------------------
Organization_id int
Country_id int
Country
-----------------------
Country_id int
CountryName string
我尝试过将其映射为多对多组件集合,但这似乎被忽略,结果与一对多映射一样,无需查找。
在不为国家/地区定义单独实体的情况下是否可以进行映射?
Let's say I have an entity Organization
with collection of countries. I don't really care about countries for anything more than displaying it with organization data, potentially for filtering, that's all. In my object model, I would prefer to have plain string
to represent countries:
class Organization
{
...
public virtual IList<string> Countries { get; set; }
...
}
That's quite easy to map as one-to-many, using collection of components that contain only country name element. This will produce table like:
OrganizationCountries
-----------------------
Organization_id int
CountryName string
But to avoid massive redundancy and to be able to easily update country names, I would like to keep country names in separate table (like lookup table), so that in the database I have:
OrganizationCountries
-----------------------
Organization_id int
Country_id int
Country
-----------------------
Country_id int
CountryName string
I've tried to map this as many-to-many collection of components, but this seems to be ignored and the result is as with one-to-many mapping, without lookup.
Is that mapping possible without defining separate entity for Countries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有一个国家/地区自己的表和 ID,那么它可以由多个组织使用。组件的定义是其生命周期取决于所属实体。在您更改的模型中情况并非如此(您可以删除组织而不删除关联的国家/地区),因此国家/地区不再是组件。
要么对组件使用一对多,要么对实体使用多对多。
If you have an own table and ID for a Country then it could be used by multiple Organizations. The definition of a component is that its lifetime depends on the owning entity. This is not true in your changed model (you could delete an Organization without deleting the associated Countries), so Country isn't a component any more.
Either use a one-to-many with components or a many-to-many with entities.