static void TestPManyToMany()
        {
            using (ModelManyToManyContainer context = new ModelManyToManyContainer())
            {
                Artist artist = new Artist()
                {
                    FirstName = "Artist FirstName ",
                    LastName  = "Artist LastName "
                };

                Artist artist2 = new Artist()
                {
                    FirstName = "Artist 2 FirstName ",
                    LastName  = "Artist 2 LastName "
                };

                Album album = new Album()
                {
                    AlbumName = "Album "
                };

                artist.Albums.Add(album);
                artist2.Albums.Add(album);

                album.Artists.Add(artist);
                album.Artists.Add(artist2);

                context.Artists.Add(artist);
                context.Artists.Add(artist2);
                context.Albums.Add(album);
                context.SaveChanges();
            }

            Console.WriteLine("Verificati baza de date pentru confirmarea testarii many to many");
        }
Exemple #2
0
        static void TestManyToMany()
        {
            using (ModelManyToManyContainer context =
                       new ModelManyToManyContainer())
            {
                Artist artist = new Artist()
                {
                    FirstName = "John2",
                    LastName  = "Doe2"
                };
                Album album = new Album()
                {
                    AlbumName = "First Album2"
                };

                context.Artists.Add(artist);
                artist.Albums.Add(album);
                context.Albums.Add(album);

                context.SaveChanges();

                var allAlbums = context.Albums;
                foreach (var a in allAlbums)
                {
                    Console.WriteLine(a + " ");
                }
            }
        }
Exemple #3
0
        public static void TestManyToMany()
        {
            using (ModelManyToManyContainer context = new ModelManyToManyContainer())
            {
                Album album = new Album
                {
                    AlbumName = "AlbumName"
                };
                Artist artist = new Artist
                {
                    FirstName = "FirstName",
                    LastName  = "LastName"
                };

                context.Albums.Add(album);
                context.Artists.Add(artist);
                context.SaveChanges();

                var albums = context.Albums;
                foreach (var alb in albums)
                {
                    Console.WriteLine("Album: {0} {1}", alb.AlbumId, alb.AlbumName);
                }

                var artists = context.Artists;
                foreach (var art in artists)
                {
                    Console.WriteLine("Artist: {0} {1} {2}", art.ArtistId, art.FirstName, art.LastName);
                }
            }
        }
Exemple #4
0
        static void TestManyToMany()
        {
            Console.WriteLine("Many to many association");

            using (ModelManyToManyContainer context = new ModelManyToManyContainer())
            {
                Album album1 = new Album()
                {
                    AlbumName = "Album 1"
                };

                Album album2 = new Album()
                {
                    AlbumName = "Album 2"
                };

                Album album3 = new Album()
                {
                    AlbumName = "Album 3"
                };

                Artist artist1 = new Artist()
                {
                    FirstName = "Mr.",
                    LastName  = "Artist 1"
                };

                Artist artist2 = new Artist()
                {
                    FirstName = "Mr.",
                    LastName  = "Artist 2"
                };

                album1.Artists.Add(artist1);
                album1.Artists.Add(artist2);

                artist2.Albums.Add(album2);
                artist2.Albums.Add(album3);

                context.Albums.Add(album1);
                context.Albums.Add(album2);
                context.Albums.Add(album3);
                context.Artists.Add(artist1);
                context.Artists.Add(artist2);

                context.SaveChanges();

                var artists = context.Artists;

                foreach (var x in artists)
                {
                    Console.WriteLine("Artist : {0} {1}", x.FirstName, x.LastName);

                    foreach (var ox in x.Albums)
                    {
                        Console.WriteLine("\tAlbum: {0}", ox.AlbumName);
                    }
                }
            }
        }
Exemple #5
0
 static void TestManyToMany()
 {
     using (ModelManyToManyContainer context = new ModelManyToManyContainer())
     {
         string answer = "";
         Console.WriteLine("Do you want to introduce an album? (y/n)");
         answer = Console.ReadLine();
         if (answer.Contains("y"))
         {
             Album album = new Album();
             Console.WriteLine("Give the album name");
             context.Albums.Add(album);
             context.SaveChanges();
         }
         Console.WriteLine("Do you want to introduce an artist? (y/n)");
         answer = Console.ReadLine();
         if (answer.Contains("y"))
         {
             Artist artist = new Artist();
             Console.WriteLine("Give the artist's  first name");
             artist.FirstName = Console.ReadLine();
             Console.WriteLine("Give the artist's last name ");
             artist.LastName = Console.ReadLine();
             context.Artists.Add(artist);
             context.SaveChanges();
         }
         Console.WriteLine("Do you want to link an album to an artist? (y/n)");
         answer = Console.ReadLine();
         if (answer.Contains("y"))
         {
             AlbumArtist albumArtist = new AlbumArtist();
             Console.WriteLine("Give the artist's  id");
             albumArtist.Artists_ArtistId = Int32.Parse(Console.ReadLine());
             Console.WriteLine("Give the album's id ");
             albumArtist.Albums_AlbumId = Int32.Parse(Console.ReadLine());
             //context.AlbumArtists.Add(albumArtist);
             Artist artist = context.Artists.FirstOrDefault(a => a.ArtistId == albumArtist.Artists_ArtistId);
             Album  album  = context.Albums.FirstOrDefault(a => a.AlbumId == albumArtist.Albums_AlbumId);
             artist.Albums.Add(album);
             album.Artists.Add(artist);
             context.Entry <Artist>(artist).CurrentValues.SetValues(artist);
             context.Entry <Album>(album).CurrentValues.SetValues(album);
             //context.Albums.First(a => a.AlbumId == albumArtist.Albums_AlbumId).Artists.Add(artist);
             //context.Artists.FirstOrDefault(a => a.ArtistId == albumArtist.Artists_ArtistId).Albums.Add(album);
             context.SaveChanges();
         }
         var items = context.Albums.Include("Artists");
         foreach (var x in items)
         {
             Console.Write("Album : {0}, {1} ",
                           x.AlbumId, x.AlbumName);
             foreach (var y in x.Artists)
             {
                 Console.Write("Artist : {0}, {1}, {2}",
                               y.ArtistId, y.FirstName, y.LastName);
             }
             Console.WriteLine();
         }
     }
 }
        static void TestManyToMany()
        {
            Console.WriteLine("\nMany to many association");

            using (ModelManyToManyContainer context =
                       new ModelManyToManyContainer())
            {
                Console.WriteLine("FirstName:");
                Console.WriteLine("LastName:");

                Artist artist1 = new Artist()
                {
                    FirstName = Console.ReadLine(),
                    LastName  = Console.ReadLine()
                };

                Console.WriteLine("Album Name");
                Album album1 = new Album()
                {
                    Name    = Console.ReadLine(),
                    Artists = new List <Artist>()
                    {
                        artist1
                    }
                };

                Console.WriteLine("Album Name");
                Album album2 = new Album()
                {
                    Name    = Console.ReadLine(),
                    Artists = new List <Artist>()
                    {
                        artist1
                    }
                };

                context.Artists.Add(artist1);
                context.Albums.Add(album1);
                context.Albums.Add(album2);
                context.SaveChanges();

                foreach (var artist in context.Artists)
                {
                    Console.WriteLine("Name: {0} {1}", artist.FirstName, artist.LastName);
                    foreach (var album in artist.Albums)
                    {
                        Console.WriteLine("Album: {0} Id: {1}", album.Id, album.Name);
                    }
                }
            }
        }