开发者

How to map many-to-many lookup values without separate entity?

开发者 https://www.devze.com 2023-04-10 11:08 出处:网络
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

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
---------开发者_如何学C--------------
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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号