Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 305
Abhijit-K Avatar asked Feb 20 '23 09:02

Abhijit-K


1 Answers

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;}
}
like image 154
Jayantha Lal Sirisena Avatar answered Mar 09 '23 00:03

Jayantha Lal Sirisena