static void TestManyToMany() { Console.WriteLine("Many to many association."); using (Model3Container context = new Model3Container()) { Artist ar1 = new Artist() { FirstName = "John", LastName = "Doe" }; Artist ar2 = new Artist() { FirstName = "Jane", LastName = "Doe" }; Album al1 = new Album() { AlbumName = "Sonata", Artists = { ar1, ar2 } }; Album al2 = new Album() { AlbumName = "Test", Artists = { ar1, ar2 } }; ar1.Albums.Add(al1); ar1.Albums.Add(al2); ar2.Albums.Add(al1); ar2.Albums.Add(al2); context.Albums.Add(al1); context.Albums.Add(al2); context.Artists.Add(ar1); context.Artists.Add(ar2); context.SaveChanges(); var items = context.Artists; foreach (var x in items) { Console.WriteLine("Artist: {0}, {1}, {2}", x.ArtistId, x.FirstName, x.LastName); foreach (var ox in x.Albums) { Console.WriteLine("\tOrders: {0}, {1}, {2}", ox.AlbumId, ox.AlbumName, ox.Artists); foreach (var oy in ox.Artists) { Console.WriteLine("\tArtist: {0}, {1}, {2}", x.ArtistId, x.FirstName, x.LastName); } } } } }
public MusicService(Model3Container context) { this.context = context; }