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

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

表关系图:

2. 创建实体模型,如图

备注:明明选了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 }

输出:

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

Entity Framework 6.x 学习之 - 创建带连接表的实体模型 with Database First,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。