Entity Framework code first, custom, additonal fields in many to many relationship

ef-code-first, entity-framework, entity-framework-4, entity-framework-4.1

Solution

It doesn't supported by default entity framework. You need to define one more entity(ChanalMedia) to represent the relationship.

public class Channel
{
    public int ChannelId {get; set;}
    public string Name {get; set;}
    public ICollection<ChanalMedia> ChanalMedias{get; set;}
}

public class ChanalMedia{
      public Channel Channel {get; set;}
      public Media Media {get; set;}
     //additional fields here..
}

public class Media
{
    public int MediaId {get; set;}
    public string Name {get; set;}
    public string Location {get; set;}
    public ICollection<ChanalMedia> ChanalMedias{get; set;}
}

Problem

In many to many relationship I want to maintain the sequence of the `Playlist` which is collection of the `Media` for a `Channel`. The sequence will be an int field from 0 to int max. ``` public class Channel { public int ChannelId {get; set;} public string Name {get; set;} public ICollection<Media> Playlist {get; set;} } public class Media { public int MediaId {get; set;} public string Name {get; set;} public string Location {get; set;} public ICollection<Channel> Channels {get; set;} } ``` Using Entity Framework code first I want to design a many to many relationship between `Channel` and `Media` so that the `Channel` will have a `Playlist` items of type Media and also maintain the order sequence. Entity Framework would add a table `ChannelMedias` for this relationship with following schema ``` ChannelMedias ----------------------------- Channel_ChannelId (int) Media_MediaId (int) ``` How can I maintain a sequence order like this: ``` ChannelMedias ------------------------------ Channel_ChannelId (int) Media_MediaId (int) MediaSequence (int) ```

Original source