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的不足越来越明显. ● 根据 ...
随机推荐
- Eclipse导入GitHub项目两处报错处理
1.项目出现Could not calculate build plan:pligin 错误解决办法: 删除本地.m2仓库中 org.apache.maven.plugins:maven-resour ...
- Solr学习笔记(2) —— Solr管理索引库
一.维护索引 1.1 添加/更新文档 1.2 批量导入数据(使用dataimport) 第一步:把mysql的数据驱动.以及dataimport插件依赖的jar包添加到solrcore(collect ...
- maven 设置 编码 ,jdk 版本
<profile> <id>jdk1.8</id> <activation> <activeByDefault>true</activ ...
- windows 远程到ubuntu桌面
Windows remote connect ubuntu desktop 1. install xRDP sudo apt-get update sudo apt-get install xrdp ...
- my.梦幻手游_XP
1.http://my.netease.com/thread-459708-1-1.html 2. 3.
- angularjs的ng-repeat回调
首先html代码是这样的: <label>Name des Leiters:</label><select name="leaderID" id=&q ...
- js学习笔记 -- await/ async
await 暂停async function函数,等待Promise处理完成,若Promise 状态为fulfilled,其回调resolve的参数作为await的值,Promise 状态为rejec ...
- OfficeControl插件的用法
项目中需要用到文档在线编辑的功能,网上找到这篇文章: http://hi.baidu.com/hurtingwings/item/bf83b6343305a94e3075a19e
- C# a == b ? c :d 表示的意思
a==b 为true,这个表达式返回c; a==b为false,这个表达式返回d. 相当于: if(a == b) { return c; } else { return d; }
- [转]使用Node.js完成的第一个项目的实践总结
本文转自:http://blog.csdn.net/yanghua_kobe/article/details/17199417 https://github.com/yanghua/FixedAsse ...