Entity Framework 6.x - 创建模型来自于现有数据库

 

Creating a Model from an Existing Database

一、创建数据库 Chapter2

USE master
GO

CREATE DATABASE Chapter2
GO
USE Chapter2
GO

CREATE TABLE [Meter]
(
[MeterId] [int] NOT NULL IDENTITY(1, 1),
[MeterName] [varchar] (30) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [Meter] ADD CONSTRAINT [PK_Meter] PRIMARY KEY CLUSTERED  ([MeterId]) ON [PRIMARY]
GO

CREATE TABLE [Poet]
(
[PoetId] [int] NOT NULL IDENTITY(1, 1),
[FirstName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT 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 [Poet] ADD CONSTRAINT [PK_Poet] PRIMARY KEY CLUSTERED  ([PoetId]) ON [PRIMARY]
GO

CREATE TABLE [Poem]
(
[PoemId] [int] NOT NULL IDENTITY(1, 1),
[PoetId] [int] NOT NULL,
[Title] [varchar] (255) COLLATE Chinese_PRC_CI_AS NOT NULL,
[MeterId] [int] NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [Poem] ADD CONSTRAINT [PK_Poem] PRIMARY KEY CLUSTERED  ([PoemId]) ON [PRIMARY]
GO
ALTER TABLE [Poem] ADD CONSTRAINT [FK_Poem_Meter] FOREIGN KEY ([MeterId]) REFERENCES [Meter] ([MeterId])
GO
ALTER TABLE [Poem] ADD CONSTRAINT [FK_Poem_Poet] FOREIGN KEY ([PoetId]) REFERENCES [Poet] ([PoetId])
GO
View Code

二、创建项目 CreateModelFromExistingDatabase

三、更新 Entity Framework 到最新版本

四、添加模型文件

完成后,得到如下图模型视图:

重新编译一下项目。

下面我们利用上面建立的模型对数据库进行操作,代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace CreateModelFromExistingDatabase
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //添加数据
14             using (var context = new Chapter2Entities())
15             {
16                 var poet = new Poet { FirstName = "John", LastName = "Milton" };
17                 var poem = new Poem { Title = "Paradise Lost" };
18                 var meter = new Meter { MeterName = "Iambic Pentameter" };
19                 poem.Meter = meter;
20                 poem.Poet = poet;
21                 context.Poems.Add(poem);
22                 poem = new Poem { Title = "Paradise Regained" };
23                 poem.Meter = meter;
24                 poem.Poet = poet;
25                 context.Poems.Add(poem);
26 
27                 poet = new Poet { FirstName = "Lewis", LastName = "Carroll" };
28                 poem = new Poem { Title = "The Hunting of the Shark" };
29                 meter = new Meter { MeterName = "Anapestic Tetrameter" };
30                 poem.Meter = meter;
31                 poem.Poet = poet;
32                 context.Poems.Add(poem);
33 
34                 poet = new Poet { FirstName = "Lord", LastName = "Byron" };
35                 poem = new Poem { Title = "Don Juan" };
36                 poem.Meter = meter;
37                 poem.Poet = poet;
38                 context.Poems.Add(poem);
39 
40                 context.SaveChanges();
41             }
42             //显示数据
43             using (var context = new Chapter2Entities())
44             {
45                 var poets = context.Poets;
46                 foreach (var poet in poets)
47                 {
48                     Console.WriteLine("{0} {1}", poet.FirstName, poet.LastName);
49                     foreach (var poem in poet.Poems)
50                     {
51                         Console.WriteLine("\t{0} ({1})", poem.Title, poem.Meter.MeterName);
52                     }
53                 }
54             }
55             Console.ReadKey();
56         }
57     }
58 }

显示结果:

 在到数据库里看一下3个表情况。

posted @ 2014-08-09 23:15  海阔天空XM  阅读(1047)  评论(0编辑  收藏  举报