多對多關係

無負載創建多對多關聯的模型

原文中是Modeling a Many-to-Many Relationship with No Payload,雖然這麼翻譯也有點不許確,可是能夠說明其目的,以下圖所示,數據庫中是這樣的關係,數據庫

咱們按照Database Frist的開發模式,由嚮導建立模型,會獲得幾個實體對象呢?spa

由此看出,這種多對多的關係賓補須要其它實體承載和轉換,那麼Modeling a Many-to-Many Relationship with No Payload也就這個意思咯。翻譯

再看看它是怎麼工做的:code

       using (var context = new EF6RecipesContext__1())
            {
                // add an artist with two albums
                var artist = new Artist { FirstName = "Alan", LastName = "Jackson" };
                var album1 = new Album { AlbumName = "Drive" };
                var album2 = new Album { AlbumName = "Live at Texas Stadium" };
                artist.Albums.Add(album1);
                artist.Albums.Add(album2);
                context.Artists.Add(artist);
                // add an album for two artists
                var artist1 = new Artist { FirstName = "Tobby", LastName = "Keith" };
                var artist2 = new Artist { FirstName = "Merle", LastName = "Haggard" };
                var album = new Album { AlbumName = "Honkytonk University" };
                artist1.Albums.Add(album);
                artist2.Albums.Add(album);
                context.Albums.Add(album);
                context.SaveChanges();
            }
            using (var context = new EF6RecipesContext__1())
            {
                Console.WriteLine("Artists and their albums...");
                var artists = context.Artists;
                foreach (var artist in artists)
                {
                    Console.WriteLine("{0} {1}", artist.FirstName, artist.LastName);
                    foreach (var album in artist.Albums)
                    {
                        Console.WriteLine("\t{0}", album.AlbumName);
                    }
                }
                Console.WriteLine("\nAlbums and their artists...");
                var albums = context.Albums;
                foreach (var album in albums)
                {
                    Console.WriteLine("{0}", album.AlbumName);
                    foreach (var artist in album.Artists)
                    {
                        Console.WriteLine("\t{0} {1}", artist.FirstName, artist.LastName);
                    }
                }
            }

創建多對多關聯的模型並添加額外的信息

上面的LinkTable只包含了兩個外鍵信息,EntityFramework並不會爲今生成一個EntityType,可是若是LinkTable中附加了一些信息,EntityFramework就會爲今生成一個EntityType,造成兩個一對多的關聯以下:對象

那麼根據嚮導生成的實體關係圖以下:blog

再看看它是如何讓工做的【書中這段代碼有點問題,現更正以下】ip

using (var context = new EF6_2RecipesContext())
            {
                var order = new Order
                {
                    OrderId = 1,
                    OrderDate = new DateTime(2010, 1, 18)
                };
                var item = new Item
                {
                    SKU = 1729,
                    Description = "Backpack",
                    Price = 29.97M
                };
                var oi = new OrderItem { Order = order, Item = item, Count = 1 };
                context.OrderItems.Add(oi);
                item = new Item
                {
                    SKU = 2929,
                    Description = "Water Filter",
                    Price = 13.97M
                };
                oi = new OrderItem { Order = order, Item = item, Count = 3 };
                context.OrderItems.Add(oi);
                item = new Item
                {
                    SKU = 1847,
                    Description = "Camp Stove",
                    Price = 43.99M
                };
                oi = new OrderItem { Order = order, Item = item, Count = 1 };
                context.OrderItems.Add(oi);
                context.SaveChanges();
            }
            using (var context = new EF6_2RecipesContext())
            {
                foreach (var order in context.Orders)
                {
                    Console.WriteLine("Order # {0}, ordered on {1}", order.OrderId,order.OrderDate);
                    Console.WriteLine("SKU\tDescription\tQty\tPrice");
                    Console.WriteLine("---\t-----------\t---\t-----");
                    foreach (var oi in order.OrderItems)
                    {
                        Console.WriteLine("{0}\t{1}\t{2}\t{3}", oi.Item.SKU,
                        oi.Item.Description, oi.Count,
                        oi.Item.Price.ToString("C"));
                    }
                }
            }

這也是書中推薦的創建多對多關係的方式,固然,項目的初期可能不須要添加額外的信息,而造成的第一種多對多的模式,那麼書中也給出了最簡單也是最優的解決方案,就是在LinkTable中額外使用一個Int類型的標識列便可,這樣,到後期就能夠體現出它的靈活性了。ci

相關文章
相關標籤/搜索