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实体,并对实体进行后续操作,适用旧项目改造。

EF--DB First的更多相关文章

  1. EF db first 获取表名称

    一直以来,使用DB FIRST的方式,想得到表名,最后一直不得其法.直到昨天晚上,反编译自己的程序集的时候,突然发现EF表结构和数据实体类的映射关系存在什么地方.然后就有了这篇文章. 咱们一步步来. ...

  2. c#.net EF DB FIRST 添加新的模型

    双击.edmx ,右键-从数据库更新模型,在“添加”里选择新表.

  3. 关于Entity Framework采用DB First模式创建后的实体批量修改相关属性技巧

    Entity Framework采用DB First模式创建实体是比较容易与方便的,修改已创建的实体在个数不多的情况下也是没问题的,但如果已创建的实体比较多,比如10个实体以上,涉及修改的地方比较多的 ...

  4. EF封装类,供参考!

    以下是我对EF DB FIRST 生成的ObjectContext类进行封装,代码如下,供参考学习: using System; using System.Collections.Generic; u ...

  5. 【EF 译文系列】重试执行策略的局限性(EF 版本至少为 6)

    原文链接:Limitations with Retrying Execution Strategies (EF6 onwards) 当使用重试执行策略的时候,大体有以下两种局限性: 不支持以流的方式进 ...

  6. Data Validate 之 Data Annotation

    什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF  Db first中使用Data Annotation asp.net MVC中使用Data ...

  7. jpeg相关知识

    一.jpeg介绍 JPEG 是 Joint Photographic Exports Group 的英文缩写,中文称之为联合图像专家小组.该小组隶属于 ISO 国际标准化组织,主要负责定制静态数字图像 ...

  8. [转]DbFirst数据验证

    转自:Data Validate 之 Data Annotation 什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF  Db first中使用 ...

  9. Entity Framework 中使用SQL Server全文索引(Full Text Search)

    GitHub:https://github.com/fissoft/Fissoft.EntityFramework.Fts EntityFramework中原来使用全文索引有些麻烦,需要使用DbCon ...

  10. 使用EntityFramework的烦恼

    我有一个应用程序,是实现数据ETL同步的,即把数据从一个db里抽取出来,经过处理后,存储到另一个db里. O/RM采用的是EF db First. 随着项目程序的开发,EF的不足越来越明显. ● 根据 ...

随机推荐

  1. 2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

    Little Boxes Problem Description Little boxes on the hillside.Little boxes made of ticky-tacky.Littl ...

  2. Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name"

    解决办法

  3. html表单笔记

    1.下面是 <form> 属性的列表: accept-charset 规定在被提交表单中使用的字符集(默认:页面字符集). action 规定向何处提交表单的地址(URL)(提交页面). ...

  4. Nginx 基本 安装..

    ubuntu 下 Nginx是高度自由化的Web服务器,它的功能是由许多模块来支持.如果使用了某个模块,这个模块使用了一些类似zlib或OpenSSL等的第三方库,那么就必须先安装这些软件.Ubunt ...

  5. C语言把字符串转换为数字

    C当中有一些函数专门用于把字符串形式转换成数值形式. printf()函数和sprintf()函数 -->通过转换说明吧数字从数字形式转换为字符串形式: scanf()函数把输入字符串转换为数值 ...

  6. sharepoint_study_13

    描述: 解决: 1.修改了密码和账户,找到对应的应用程序池,修改用户名和密码,重启iis. 2.站点上安装的产品(如:工作流)启动需要用户名和密码,找到对应的服务,修改用户名和密码并重启该服务.

  7. TXT文件导入到ORACLE数据库中

    --创建表 (sqlplus执行) drop table cjw; ),phone ),city ),born ),adressJob ),mail )); ### txt导入到oracle cat ...

  8. nginx + uwsgi 配置参考

    参考 http://www.runoob.com/django/django-nginx-uwsgi.html ####### 20181029 cd ~wget http://python.org/ ...

  9. inventor安装不了

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  10. Unity [SerializeField]

    在Unity3d中Unity3D 中提供了非常方便的功能可以帮助用户将 成员变量 在Inspector中显示,并且定义Serialize关系. 也就是说凡是显示在Inspector 中的属性都同时具有 ...