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的不足越来越明显. ● 根据 ...
随机推荐
- c++构造函数问题,初始化和赋值问题
默认构造函数(就是没有参数的构造函数) The Default ConstructorThe default constructor is the constructor used to create ...
- asp.net core 简化模型验证 modelState.IsValid不用每一个写
第一种:直接在执行action之前验证模型 实现 IActionFilter public class ModelStateFilter : IActionFilter { public void O ...
- python 对三维CT数据缩放
项目需要对CT数据进行缩放,这里我存储CT数据的格式是numpy数组. 一共尝试了三种方法,分别是numpy.resize,cv2.resize,scipy.ndimage.interpolation ...
- FlowLayout(流式布局)用法
https://blog.csdn.net/liujun13579/article/details/7771191
- C#面试题-递归
有一组数1.1.2.3.5.8.13.21.34...,求第n个数是多少? public int Recursion (int n){ ){ ; } &&n<=){ ; } )+ ...
- window.open 设置高和宽无效
当设置_self属性时,再设置宽和高就不管用,这个宽高会继承父窗口的宽高! window.open("url","_self","width=100, ...
- P1111 修复公路(并查集)
题目背景 AA地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 题目描述 给出A地区的村庄数NN,和公路数MM,公路是双向的.并告诉你每条公路的连着哪两个村庄,并告诉你 ...
- 对bootstrap模态框的小尝试
bootstrap中有一个“模态框”插件,我理解的意思就是一个具有全局遮罩的弹窗提示,官方解释是:模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗 ...
- 利用Content-disposition实现无刷新下载图片文件
今天在使用 tinypng.com 这个在线压缩图片的网站时,对其处理完图片后,可以无刷新下载图片感到好奇,于是了解了一下相关实现.无刷新下载可以利用MIME type或者设置Content-disp ...
- C++二维数组的动态声明
int **a = new int* [m] //分配一个指针数组,将其首地址保存在a中 . for(int i = 0; i < m; i++) //为指针数组的每个元素分配一 ...