I have a simple architectural question and would love some advice.
I am trying to get familiar with ASP.NET MVC 3. To do so I am coding a small TV Shows gallery as an exercise.
My Models folder holds several classes, including a TvShow, a Season and an Episode class. Those are naturally tied together: a TvShow instance has a one-to-many relationship with Season instances while a Season instance has a one-to-many relationship with Episode instances.
Using ASP.NET’s Entity Framework, how should I model these one-to-many relationships ? I see 2 options:
- Option 1 – The
TvShowclass has aList<Season> seasonsfield - Option 2 – The
Seasonclass has aTvShow tvShowfield
Option 2 seems more natural to me because it mimics the underlying database design. It is also the choice made by Microsoft in their MvcMusicStore tutorial. However, I’m wondering if there is a reason to reject Option 1 – either for database / EF related matters or for OO-design matters.
Thoughts?
Option 1 and Option 2 are actually compatible 😉 With Entity Framework, there’s something called navigation properties that allows to have both in case of a one-to-many association.
TvShowwill have a navigation property that contains the list ofSeasons, andSeasonwill have a navigation property that contains the associatedTvShow.