0

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 TvShow class has a List<Season> seasons field
  • Option 2 - The Season class has a TvShow tvShow field

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?

guidupuy
  • 505
  • 3
  • 15

2 Answers2

1

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.

TvShow will have a navigation property that contains the list of Seasons, and Season will have a navigation property that contains the associated TvShow.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • As simple as it gets. That's great! I'm using a Code First approach, so I guess I should combine Option 1 and 2 in my Model? – guidupuy Jan 04 '12 at 09:45
  • Also, are navigation properties "recursive", i.e. can I add a `TvShow` navigation property to my `Episode` class? – guidupuy Jan 04 '12 at 09:49
  • I personally never used code first approach yet, but it should be simple to use navigation properties with this approach. See following SO questions: http://stackoverflow.com/questions/4561079/entity-framework-ctp5-code-first-optional-navigation-property http://stackoverflow.com/questions/5534891/entity-framework-4-1-code-first-keys-navigation-properties http://stackoverflow.com/questions/6144163/entity-framework-4-1-code-first-navigation-property-not-loading-when-only-the-i – ken2k Jan 04 '12 at 09:50
  • For your second question, I'm not aware of such a possibility. Anyway, you could easily create an `ICollection` property in your `TvShow` POCO and return the list of associated episodes using a LINQ request. – ken2k Jan 04 '12 at 09:52
0

As Said, you should use both.

Don't forget that your manipulating your model and that it's only an object abstraction, it is not representative of the way it is stored. It is natural for a TvShow to have a property with the list of Seasons and for one season to have a property that references the parent TvShow.

The only important thing to know in this relations, is who is the master responsible for the update.

you should have something like that:

public class TvShow
{
   public int Id {get;set;}
   public virtual ICollection<Season> Seasons {get;set;}
}

public class Season
{
   public int Id {get;set;}
   public int TvShowId {get;set;}
   public virtual TvShow Show {get;set;}
}

Then you should help your mapping with a configuration file:

public class SeasonConfiguration : EntityTypeConfiguration<Season>
{
    internal SeasonConfiguration()
    {
        this.HasRequired(s => s.Show)
            .WithMany(tv => tv.Seasons)
            .HasForeignKey(s => s.TvShowId);
    }
}

The semantics are not the most natural but it works like that.

And if your are learning ASP.NET MVC, don't forget that what your defining here is your model and your mapping, it should be different than what you want to use in your views (ie: the ViewModel)