多对多关系<EntityFramework6.0>
2018-06-17 20:20:41来源:未知 阅读 ()
无负载建立多对多关联的模型
原文中是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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Delphi XE 10.2.3使用CEF4Delphi取网页元素时碰到&n 2018-12-09
- Delphi 10.3 Rio + iOS 12.1 SDK 编译错误 "libcha 2018-11-29
- .NET “底层”异步编程模式—&a 2018-06-21
- 【转】ASP.NET"正在中止线程"错误原因 2018-06-21
- "类工厂模式"改写SqlHelper 2018-06-21
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash