EF--DB First
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的更多相关文章
- EF db first 获取表名称
一直以来,使用DB FIRST的方式,想得到表名,最后一直不得其法.直到昨天晚上,反编译自己的程序集的时候,突然发现EF表结构和数据实体类的映射关系存在什么地方.然后就有了这篇文章. 咱们一步步来. ...
- c#.net EF DB FIRST 添加新的模型
双击.edmx ,右键-从数据库更新模型,在“添加”里选择新表.
- 关于Entity Framework采用DB First模式创建后的实体批量修改相关属性技巧
Entity Framework采用DB First模式创建实体是比较容易与方便的,修改已创建的实体在个数不多的情况下也是没问题的,但如果已创建的实体比较多,比如10个实体以上,涉及修改的地方比较多的 ...
- EF封装类,供参考!
以下是我对EF DB FIRST 生成的ObjectContext类进行封装,代码如下,供参考学习: using System; using System.Collections.Generic; u ...
- 【EF 译文系列】重试执行策略的局限性(EF 版本至少为 6)
原文链接:Limitations with Retrying Execution Strategies (EF6 onwards) 当使用重试执行策略的时候,大体有以下两种局限性: 不支持以流的方式进 ...
- Data Validate 之 Data Annotation
什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF Db first中使用Data Annotation asp.net MVC中使用Data ...
- jpeg相关知识
一.jpeg介绍 JPEG 是 Joint Photographic Exports Group 的英文缩写,中文称之为联合图像专家小组.该小组隶属于 ISO 国际标准化组织,主要负责定制静态数字图像 ...
- [转]DbFirst数据验证
转自:Data Validate 之 Data Annotation 什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF Db first中使用 ...
- Entity Framework 中使用SQL Server全文索引(Full Text Search)
GitHub:https://github.com/fissoft/Fissoft.EntityFramework.Fts EntityFramework中原来使用全文索引有些麻烦,需要使用DbCon ...
- 使用EntityFramework的烦恼
我有一个应用程序,是实现数据ETL同步的,即把数据从一个db里抽取出来,经过处理后,存储到另一个db里. O/RM采用的是EF db First. 随着项目程序的开发,EF的不足越来越明显. ● 根据 ...
随机推荐
- linux 基本概念
Linux把物理内存分为了固定统一大小的块,称为page(页框),一般为4KB. Linux采用4KB页框大小作为标准的物理内存分配单元,内核用数据结构page描述一个页框的状态信息,其实页是进程的概 ...
- CF959C Mahmoud and Ehab and the wrong algorithm 构造
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is: Given an un ...
- 7.Palindrome Linked List(回文链表)
Level: Easy 题目描述: Given a singly linked list, determine if it is a palindrome. Example 1: Input: ...
- SharePoint开发中可能用到的各种Context(上下文)
转载: http://www.cnblogs.com/erucy/archive/2012/08/25/2655600.html 电脑正在以无比慢的速度从微软网站上安装Office Component ...
- iOS如何实时查看App运行日志
Linux下管理挂载IOS设备——libimobiledevicehttps://www.jianshu.com/p/6423610d3293https://blog.csdn.net/fengzei ...
- jenkins出现的问题
1.增加服务器时 要修改时,,需要设置 2:出现这个问题是 执行了npm install node-sass
- Problem04 分解质因数
题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5. 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: (1)如果这个质数恰等于n,则说明分解质因数的 ...
- xml和json互转
开发过程中有些时候需要把xml和json互转,如某钱X接口入参和出参都是xml格式的,十分蛋疼.特写下面工具类,以留用. 需要引用jar: <!-- https://mvnrepository. ...
- OS---文件结构
1.概述 1.1 对于任何一个文件,都存在以下2种形式结构: 文件的逻辑结构: 从用户的角度出发所观察到的文件组织形式,独立于文件的物理特性: 文件的物理结构(文件存储结构): 文件在外存上的存储组织 ...
- 数据结构---Java---LinkedList
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, D ...