博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Entity Framework 6.x - 创建模型来自于现有数据库
阅读量:5262 次
发布时间:2019-06-14

本文共 2901 字,大约阅读时间需要 9 分钟。

 

Creating a Model from an Existing Database

一、创建数据库 Chapter2

USE masterGOCREATE DATABASE Chapter2GOUSE Chapter2GOCREATE TABLE [Meter]([MeterId] [int] NOT NULL IDENTITY(1, 1),[MeterName] [varchar] (30) COLLATE Chinese_PRC_CI_AS NOT NULL) ON [PRIMARY]GOALTER TABLE [Meter] ADD CONSTRAINT [PK_Meter] PRIMARY KEY CLUSTERED  ([MeterId]) ON [PRIMARY]GOCREATE 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]GOALTER TABLE [Poet] ADD CONSTRAINT [PK_Poet] PRIMARY KEY CLUSTERED  ([PoetId]) ON [PRIMARY]GOCREATE 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]GOALTER TABLE [Poem] ADD CONSTRAINT [PK_Poem] PRIMARY KEY CLUSTERED  ([PoemId]) ON [PRIMARY]GOALTER TABLE [Poem] ADD CONSTRAINT [FK_Poem_Meter] FOREIGN KEY ([MeterId]) REFERENCES [Meter] ([MeterId])GOALTER 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个表情况。

转载于:https://www.cnblogs.com/wanghaibin/p/3901756.html

你可能感兴趣的文章
linux grep 搜索查找
查看>>
Not enough free disk space on disk '/boot'(转载)
查看>>
android 签名
查看>>
android:scaleType属性
查看>>
SuperEPC
查看>>
mysql-5.7 innodb 的并行任务调度详解
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
Js时间处理
查看>>
Java项目xml相关配置
查看>>
三维变换概述
查看>>
vue route 跳转
查看>>
【雷电】源代码分析(二)-- 进入游戏攻击
查看>>
Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。...
查看>>
Linux中防火墙centos
查看>>
mysql新建用户,用户授权,删除用户,修改密码
查看>>
FancyCoverFlow
查看>>
JS博客
查看>>
如何设置映射网络驱动器的具体步骤和方法
查看>>
ASP.NET WebApi 基于OAuth2.0实现Token签名认证
查看>>