Entity Framework 6.x 学习之 - 创建带连接表的实体模型 with Database First

一、Modeling a Many-to-Many Relationship with No Payload

1. 创建数据库表

CREATE TABLE [Album]

(

[AlbumId] [int] NOT NULL IDENTITY(1, 1),

[AlbumName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL

) ON [PRIMARY]

GO

ALTER TABLE [Album] ADD CONSTRAINT [PK_Album] PRIMARY KEY CLUSTERED  ([AlbumId]) ON [PRIMARY]

GO



CREATE TABLE [Artist]

(

[ArtistId] [int] NOT NULL IDENTITY(1, 1),

[FirstName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,

[MiddleName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL,

[LastName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL

) ON [PRIMARY]

GO

ALTER TABLE [Artist] ADD CONSTRAINT [PK_Artist] PRIMARY KEY CLUSTERED  ([ArtistId]) ON [PRIMARY]

GO



CREATE TABLE [LinkTable]

(

[ArtistId] [int] NOT NULL,

[AlbumId] [int] NOT NULL

) ON [PRIMARY]

GO

ALTER TABLE [LinkTable] ADD CONSTRAINT [PK_LinkTable] PRIMARY KEY CLUSTERED  ([ArtistId], [AlbumId]) ON [PRIMARY]

GO

ALTER TABLE [LinkTable] ADD CONSTRAINT [FK_LinkTable_Album] FOREIGN KEY ([AlbumId]) REFERENCES [Album] ([AlbumId])

GO

ALTER TABLE [LinkTable] ADD CONSTRAINT [FK_LinkTable_Artist] FOREIGN KEY ([ArtistId]) REFERENCES [Artist] ([ArtistId])

GO
Create Table

表关系图:

Entity Framework 6.x 学习之 - 创建带连接表的实体模型 with Database First

2. 创建实体模型,如图

Entity Framework 6.x 学习之 - 创建带连接表的实体模型 with Database First

备注:明明选了3个表,为什么只有2个实体呢?

因为中间那个表为连接表,所以在映射实体的时候只有2个实体,这种情况叫 no payload 。

为什么连接表不会被创建成一个实体呢?

因为他没有自己的字段,如果给链接表加一个字段,那么创建的实体模型就会和数据库表关系一样,一个一个的对应上。

3. 操作实体类

 1 using System;

 2 

 3 namespace ConsoleApplication3

 4 {

 5     class Program

 6     {

 7         static void Main(string[] args)

 8         {

 9             //Insert

10             using (var context = new test2Entities())

11             {

12                 //添加一个艺术家属于2个专辑

13                 var artist = new Artist { FirstName = "Alan", LastName = "Jackson" };

14                 var album1 = new Album { AlbumName = "Drive" };

15                 var album2 = new Album { AlbumName = "Live at Texas Stadium" };

16                 artist.Albums.Add(album1);

17                 artist.Albums.Add(album2);

18                 context.Artists.Add(artist);

19 

20                 //添加一个专辑有2个艺术家

21                 var artist1 = new Artist { FirstName = "Tobby", LastName = "Keith" };

22                 var artist2 = new Artist { FirstName = "Merle", LastName = "Haggard" };

23                 var album = new Album { AlbumName = "Honkytonk University" };

24                 artist1.Albums.Add(album);

25                 artist2.Albums.Add(album);

26                 context.Albums.Add(album);

27 

28                 context.SaveChanges();

29             }

30 

31             //Select

32             using (var context = new test2Entities())

33             {

34                 Console.WriteLine("艺术家和他们的专辑...");

35                 var artists = context.Artists;

36                 foreach (var artist in artists)

37                 {

38                     Console.WriteLine("{0} {1}", artist.FirstName, artist.LastName);

39                     foreach (var album in artist.Albums)

40                     {

41                         Console.WriteLine("\t{0}", album.AlbumName);

42                     }

43                 }

44 

45                 Console.WriteLine("\n专辑中的艺术家...");

46                 var albums = context.Albums;

47                 foreach (var album in albums)

48                 {

49                     Console.WriteLine("{0}", album.AlbumName);

50                     foreach (var artist in album.Artists)

51                     {

52                         Console.WriteLine("\t{0} {1}", artist.FirstName, artist.LastName);

53                     }

54                 }

55             }

56             Console.ReadKey();

57         }

58     }

59 }

输出:
Entity Framework 6.x 学习之 - 创建带连接表的实体模型 with Database First

程序运行3次后的数据库表结果:

Entity Framework 6.x 学习之 - 创建带连接表的实体模型 with Database First

二、Modeling a Many-to-Many Relationship with a Payload

1. 创建数据库表:

CREATE TABLE [Order]

(

[OrderId] [int] NOT NULL,

[OrderDate] [datetime] NOT NULL

) ON [PRIMARY]

GO



ALTER TABLE [Order] ADD CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED  ([OrderId]) ON [PRIMARY]

GO

CREATE TABLE [Item]

(

[SKU] [int] NOT NULL,

[Description] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL,

[Price] [decimal] (18, 2) NOT NULL

) ON [PRIMARY]

GO

ALTER TABLE [Item] ADD CONSTRAINT [PK_Item] PRIMARY KEY CLUSTERED  ([SKU]) ON [PRIMARY]

GO

CREATE TABLE [OrderItem]

(

[OrderId] [int] NOT NULL,

[SKU] [int] NOT NULL,

[Count] [int] NOT NULL

) ON [PRIMARY]

GO

ALTER TABLE [OrderItem] ADD CONSTRAINT [PK_OrderItem] PRIMARY KEY CLUSTERED  ([OrderId], [SKU]) ON [PRIMARY]

GO

ALTER TABLE [OrderItem] ADD CONSTRAINT [FK_OrderItem_Item] FOREIGN KEY ([SKU]) REFERENCES [Item] ([SKU])

GO

ALTER TABLE [OrderItem] ADD CONSTRAINT [FK_OrderItem_Order] FOREIGN KEY ([OrderId]) REFERENCES [Order] ([OrderId])

GO
Create Table

表关系图:
Entity Framework 6.x 学习之 - 创建带连接表的实体模型 with Database First
创建模型图:
Entity Framework 6.x 学习之 - 创建带连接表的实体模型 with Database First
这回连接表有了实体了,为什么呢?因为OrderItem表有自己的字段Count

3. 操作实体类:

using System;



namespace ConsoleApplication4

{

    class Program

    {

        static void Main(string[] args)

        {

            //Add

            using (var context = new test2Entities())

            {

                var order = new Order { OrderId = 100, OrderDate = new DateTime(2010, 1, 18) };



                var item = new Item { SKU = 11729, Description = "Backpack", Price = 29.97M };

                var oi = new OrderItem { Order = order, Item = item, Count = 1 };

                context.Items.Add(item);

                context.OrderItems.Add(oi);



                item = new Item { SKU = 12929, Description = "Water Filter", Price = 13.97M };

                oi = new OrderItem { Order = order, Item = item, Count = 3 };

                context.Items.Add(item);

                context.OrderItems.Add(oi);



                item = new Item { SKU = 11847, Description = "Camp Stove", Price = 43.99M };

                oi = new OrderItem { Order = order, Item = item, Count = 1 };

                context.Items.Add(item);

                context.OrderItems.Add(oi);



                context.Orders.Add(order);

                context.SaveChanges();

            }



            //Select

            using (var context = new test2Entities())

            {

                foreach (var order in context.Orders)

                {

                    Console.WriteLine("Order # {0}, ordered on {1}", order.OrderId.ToString(), order.OrderDate.ToShortDateString());

                    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.ToString(), oi.Item.Price.ToString("C"));

                    }

                }

            }

            Console.ReadKey();

        }

    }

}

 

你可能感兴趣的:(framework)