多对多关系<EntityFramework6.0>

2018-06-18 03:12:12来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

无负载建立多对多关联的模型

原文中是Modeling a Many-to-Many Relationship with No Payload,虽然这么翻译也有点不准确,但是可以说明其目的,如下图所示,数据库中是这样的关系,

我们按照Database Frist的开发模式,由向导创建模型,会得到几个实体对象呢?

由此看出,这种多对多的关系宾补需要其它实体承载和转换,那么Modeling a Many-to-Many Relationship with No Payload也就这个意思咯。

再看看它是怎么工作的:

       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,形成两个一对多的关联如下:

那么根据向导生成的实体关系图如下:

再看看它是如何让工作的【书中这段代码有点问题,现更正如下】

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类型的标识列即可,这样,到后期就可以体现出它的灵活性了。

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C#程序集系列12,C#编译器和CLR如何找寻程序集

下一篇:如何在ashx页面获取Session值