多对多关系

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

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

多对多关系<EntityFramework6.0>

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

多对多关系<EntityFramework6.0>

由此看出,这种多对多的关系宾补需要其它实体承载和转换,那么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,形成两个一对多的关联如下:

多对多关系<EntityFramework6.0>

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

多对多关系<EntityFramework6.0>

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

using (var context = new EF6_2RecipesContext())
{
var order = new Order
{
OrderId = ,
OrderDate = new DateTime(, , )
};
var item = new Item
{
SKU = ,
Description = "Backpack",
Price = 29.97M
};
var oi = new OrderItem { Order = order, Item = item, Count = };
context.OrderItems.Add(oi);
item = new Item
{
SKU = ,
Description = "Water Filter",
Price = 13.97M
};
oi = new OrderItem { Order = order, Item = item, Count = };
context.OrderItems.Add(oi);
item = new Item
{
SKU = ,
Description = "Camp Stove",
Price = 43.99M
};
oi = new OrderItem { Order = order, Item = item, Count = };
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类型的标识列即可,这样,到后期就可以体现出它的灵活性了。

上一篇:【IT名人堂】何云飞:阿里云数据库的架构演进之路


下一篇:【emWin】例程三:显示方向的切换