DB First先有数据库,根据数据库生成Model实体对象。

1、新建数据库表,Poet,Poem,Meter.关系如下:

  

  建表语句

create table Poet
(
PoetId int identity(,) primary key,
FirstName varchar(),
MiddleName varchar(),
LastName varchar()
) create table Meter
(
MeterId int identity(,) primary key,
MeterName varchar()
) create table Poem
(
poemId int identity(,) primary key,
PoetId int,
MeterId int,
Title varchar(),
foreign key(PoetId) references poet(Poetid),
foreign key(MeterId) references Meter(MeterId)
) create view vwLibrary
as
select poet.FirstName,poet.MiddleName,poet.LastName,poem.Title,Meter.MeterName from Meter ,poet,Poem where Meter.MeterId=poem.MeterId and poem.PoetId=poet.PoetId

2、新建控制台项目DBFirstDemo,确定。

3、点击选中项目添加-->新建-->选择数据模板-->ADO.NET实体数据模型,确定。

  

4、实体模型向导选择从数据库生成,下一步。

  

5、选择或者新建链接,连接选择后继续下一步。

  

6、选择框架版本,本机选择6.0

  

7、选择数据库对象,选择表和视图。并勾选下方复选框。

  

8、生成实体图上展示如下:

  

9、使用实体,修改Main方法如下:

  

static void Main(string[] args)
{
using (var context = new EF6RecipesEntities())
{
var poet = new poet { FirstName = "John", LastName = "Milton" };
var poem = new Poem { Title = "Paradis Lost" };
var meter = new Meter { MeterName = "Iambic Pentameter" }; poem.poet = poet;
poem.Meter = meter; context.Poems.Add(poem); poem = new Poem { Title="Paradis Regained" };
poem.poet = poet;
poem.Meter = meter; context.Poems.Add(poem); poet = new poet { FirstName = "Lewis", LastName = "Carroll" };
poem = new Poem { Title = "The Hunting of the Shark" };
meter = new Meter { MeterName = "Anapestic Tetrameter" };
poem.Meter = meter;
poem.poet = poet;
context.Poems.Add(poem);
poet = new poet { FirstName = "Lord", LastName = "Byron" };
poem = new Poem { Title = "Don Juan" };
poem.Meter = meter;
poem.poet = poet;
context.Poems.Add(poem);
context.SaveChanges(); Console.WriteLine("----------------读取Poet----------------"); var poets = context.poets; foreach (var poettemp in poets)
{
Console.WriteLine("{0}{1}", poettemp.FirstName, poettemp.LastName);
foreach (var poemtemp in poet.Poems)
{
Console.WriteLine("\t{0} ({1})", poemtemp.Title, poemtemp.Meter.MeterName);
}
} Console.ReadKey();
}
}

10、执行结果

  

总结:DBFirst是根据现有数据库生成Model实体,并对实体进行后续操作,适用旧项目改造。

最新文章

  1. laravel框架总结(十四) -- 数据迁移和数据填充
  2. 支付宝App支付签名和验签
  3. Mysql 查看连接数,状态
  4. php获取在线xml的数据
  5. 35.在PCB中删除元件
  6. 2016年中国500强利润率最高的公司,中国500强最赚钱的40家公司,ROE最高的公司
  7. Android学习路线(二十七)键值对(SharedPreferences)存储
  8. struts2 全局格式化,格式化时间,金钱,数字
  9. codeforce-748A
  10. xml序列化和反序列化(一)
  11. 万能的Python,还能用来制作高大上的进度条?
  12. 记录Nginx作为静态资源web服务场景配置
  13. 从零开始学习PYTHON3讲义(三)写第一个程序
  14. ELK简单安装测试
  15. loadrunner-参数化
  16. 【c】多级指针
  17. 教你一招:解决Windows 开机弹出AotuIt ERROR 错误
  18. R语言学习 第四篇:函数和流程控制
  19. javascript柯里化
  20. java Arrays.asList 问题

热门文章

  1. Leetcode 566. Reshape the Matrix 矩阵变形(数组,模拟,矩阵操作)
  2. kuangbin专题七 POJ3468 A Simple Problem with Integers (线段树或树状数组)
  3. k-sum 问题
  4. 1001 害死人不偿命的(3n+1)猜想 (15)(15 分)
  5. N - 嘤嘤嘤 (并查集+枚举)
  6. Educational Codeforces Round 7 B
  7. Jenkins自动化CI CD流水线之7--流水线自动化发布PHP项目
  8. python学习3(转载)
  9. 使用bootstrap创建上传文件
  10. 单片机的图形UI