Entity Framework 6 Recipes 2nd Edition(10-7)译 -> TPH继承模型中使用存储过程
10-7. TPH继承模型中使用存储过程
问题
用一个存储过程来填充TPH继承模型的实体
解决方案
假设已有如Figure 10-7所示模型. 我们有两个派生实体: Instructor(教员)和Student(学生).
这个模型使用TPH继承方式,所以数据库中只有一个表. Person(人员)表有一个鉴别列,用来把表的记录映射到不同的派生实体上. 我们想用一个存储过程来填充实体.
Figure 10-7. A model for instructors and students
用下面的步骤,达到用一个存储过程来返回实体的目的:
1.在你的数据库中创建如Listing 10-18所示的存储过程. 这个存储过程返回所有人员.
Listing 10-18. The GetAllPeople Stored Procedure, Which Returns All the People, Both
Students and Instructors, in the Model
create procedure [Chapter10].[GetAllPeople]
as
begin
select * from chapter10.Person
end
2. 右击模型的设计视图,选择“从数据库更新模型”.选择存储过程GetAllPeople. 单击“完成”,把存储过程添加到模型中.
3. ( 译注:我的环境是win10+vs2013+ef6.1.3,是不需要这步的,第1步已经把这步也完成了,只是最后的“返回以下内容的集合”必须修改一下),右击模型的设计视图, 选择“新增“ ➤ 函数导入. 从“存储过程/函数名称”下拉框中选择GetAllPeople. 在“函数导入名称“文本框中输入:GetAllPeople. 这个就是在模型中的方法名称.在“返回以下内容的集合”里勾选“实体”,在下拉框里选择Person.单击“确定”.将会创建<FunctionImportMapping>框架.
4. 右击 .edmx 文件, 选择“打开方式” ➤ XML Editor.编辑.edmx文件的mapping 小节下
的<FunctionImportMapping> 标签,用 Listing 10-19所示代码去匹配(因为像EF6RecipesModel的命名与你的例子可能不同).它会映射被存储过程返回的列与Person类型的实体的属性。
(译注:所给的示例数据库里的PersonType列,类型为varchar,不可为空。我自己跟着这里所说的步骤做了一下,运行后发现有错误。看了原书网站上下载的代码后发现这一列为nvarchar,并且可为空,所以改了数据库之后,再“从数据库更新模型”一次,就可以了。)
Listing 10-19. The FunctionImportMapping Conditionally Maps Rows to Either the
Instructor or Student Entity
<FunctionImportMapping FunctionImportName="GetAllPeople"
FunctionName="EF6RecipesModel.Store.GetAllPeople">
<ResultMapping>
<EntityTypeMapping TypeName="EFRecipesModel.Student">
<ScalarProperty Name="Degree" ColumnName="Degree" />
<Condition ColumnName="PersonType" Value="Student"/>
</EntityTypeMapping>
<EntityTypeMapping TypeName="EF6RecipesModel.Instructor">
<ScalarProperty Name="Salary" ColumnName="Salary"/>
<Condition ColumnName="PersonType" Value="Instructor"/>
</EntityTypeMapping>
</ResultMapping>
</FunctionImportMapping>
5. Follow the pattern in Listing 10-20 to use the GetAllPeople stored procedure via the
GetAllPeople() method
Listing 10-20. Querying the Model Using the GetAllPeople Stored Procedure via
the GetAllPeople() Method.
static void Main(string[] args)
{
using (var context = new EFRecipesEntities1007())
{
context.People.Add(new Instructor
{
Name = "Karen Stanford",
Salary = 62500M
});
context.People.Add(new Instructor
{
Name = "Robert Morris",
Salary = 61800M
});
context.People.Add(new Student
{
Name = "Jill Mathers",
Degree = "Computer Science"
});
context.People.Add(new Student
{
Name = "Steven Kennedy",
Degree = "Math"
});
context.SaveChanges();
}
using (var context = new EFRecipesEntities1007())
{
Console.WriteLine("Instructors and Students");
var allPeople = context.GetAllPeople();
foreach (var person in allPeople)
{
if (person is Instructor)
Console.WriteLine("Instructor {0} makes {1}/year",
person.Name,
((Instructor)person).Salary);
else if (person is Student)
Console.WriteLine("Student {0}'s major is {1}",
person.Name, ((Student)person).Degree);
}
}
Console.WriteLine("\npress any key to exit...");
Console.ReadKey();
}
输出结果如Listing 10-20:
===================================================================
Instructors and Students
Instructor Karen Stanford makes $62,500.00/year
Instructor Robert Morris makes $61,800.00/year
Student Jill Mathers's major is Computer Science
Student Steven Kennedy's major is Math
=================================================================================
它是如何工作的
用一个存储过程填充TP继承模型的实体,比TPT (见10-6小节)要简单些. 这里的存储过程只是检索出表Person的所有行. 我们在<FunctionImportMapping>标签里对应关系(列名+值来确定是某种实体),(见 Listing 10-19),就是把表的鉴别列(PersonType)的不同的值,转化为相对应的实体
.在10-6小节我们这么做,在本小节,我们也是这么做。
Entity Framework 6 Recipes 2nd Edition(10-7)译 -> TPH继承模型中使用存储过程的更多相关文章
- Entity Framework 6 Recipes 2nd Edition 译 -> 目录 -持续更新
因为看了<Entity Framework 6 Recipes 2nd Edition>这本书前面8章的翻译,感谢china_fucan. 从第九章开始,我是边看边译的,没有通读,加之英语 ...
- Entity Framework 6 Recipes 2nd Edition(10-6)译 -> TPT继承模型中使用存储过程
10-6. TPT继承模型中使用存储过程 问题 想在一个TPT继承模型中使用存储过程 解决方案 假设已有如Figure 10-6所示模型. 在模型里, Magazine(杂志) and DVD继承于基 ...
- Entity Framework 6 Recipes 2nd Edition(9-3)译->找出Web API中发生了什么变化
9-3. 找出Web API中发生了什么变化 问题 想通过基于REST的Web API服务对数据库进行插入,删除和修改对象图,而不必为每个实体类编写单独的更新方法. 此外, 用EF6的Code Fri ...
- Entity Framework 6 Recipes 2nd Edition(9-4)译->Web API 的客户端实现修改跟踪
9-4. Web API 的客户端实现修改跟踪 问题 我们想通过客户端更新实体类,调用基于REST的Web API 服务实现把一个对象图的插入.删除和修改等数据库操作.此外, 我们想通过EF6的Cod ...
- Entity Framework 6 Recipes 2nd Edition(9-1)译->用Web Api更新单独分离的实体
第九章 在N层结构的应用程序中使用EF 不是所有的应用都能完全地写入到一个单个的过程中(就是驻留在一个单一的物理层中),实际上,在当今不断发展的网络世界,大量的应用程序的结构包含经典的表现层,应用程, ...
- Entity Framework 6 Recipes 2nd Edition(13-4)译 -> 有效地创建一个搜索查询
问题 你想用LINQ写一个搜索查询,能被转换成更有效率的SQL.另外,你想用EF的CodeFirst方式实现. 解决方案 假设你有如下Figure 13-6所示的模型 Figure 13-6. A s ...
- Entity Framework 6 Recipes 2nd Edition(13-2)译 -> 用实体键获取一个单独的实体
问题 不管你用DBFirst,ModelFirst或是CodeFirst的方式,你想用实体键获取一个单独的实体.在本例中,我们用CodeFirst的方式. 解决方案 假设你有一个模型表示一个Paint ...
- Entity Framework 6 Recipes 2nd Edition(13-3)译 -> 为一个只读的访问获取实体
问题 你想有效地获取只是用来显示不会更新的操作的实体.另外,你想用CodeFirst的方式来实现 解决方案 一个非常常见行为,尤其是网站,就是只是让用户浏览数据.大多数情况下,用户不会更新数据.在这种 ...
- Entity Framework 6 Recipes 2nd Edition(13-5)译 -> 使POCO的修改追踪更高
问题 你正在使用POCO,你想提高修改跟踪的性能,同时使内存消耗更少.另外,你想通过EF的CodeFirst方式来实现. 解决方案 假设你有一个关于Account(帐户)和相关的Payments(支付 ...
随机推荐
- 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新
[原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...
- SQL Server 常用内置函数(built-in)持续整理
本文用于收集在运维中经常使用的系统内置函数,持续整理中 一,常用Metadata函数 1,查看数据库的ID和Name db_id(‘DB Name’),db_name('DB ID') 2,查看对象的 ...
- 【翻译】MongoDB指南/聚合——聚合管道
[原文地址]https://docs.mongodb.com/manual/ 聚合 聚合操作处理数据记录并返回计算后的结果.聚合操作将多个文档分组,并能对已分组的数据执行一系列操作而返回单一结果.Mo ...
- 【SSM框架】Spring + Springmvc + Mybatis 基本框架搭建集成教程
本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例 说明:1.本文暂未使用maven集成,jar包需要手动导入. 2.本文为基础教程,大神切勿见笑. 3.如果对您学习有帮助,欢迎各种转载,注 ...
- npm package.json属性详解
概述 本文档是自己看官方文档的理解+翻译,内容是package.json配置里边的属性含义.package.json必须是一个严格的json文件,而不仅仅是js里边的一个对象.其中很多属性可以通过np ...
- 缓存、队列(Memcached、redis、RabbitMQ)
本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...
- C# 泛型
C# 泛型 1.定义泛型类 在类定义中包含尖括号语法,即可创建泛型类: class MyGenericClass<T> { //Add code } 其中T可以遵循C#命名规则的任意字符. ...
- C++常见笔试面试要点以及常见问题
1. C++常见笔试面试要点: C++语言相关: (1) 虚函数(多态)的内部实现 (2) 智能指针用过哪些?shared_ptr和unique_ptr用的时候需要注意什么?shared_ptr的实现 ...
- H3 BPM引擎API接口
引擎API接口通过 Engine 对象进行访问,这个是唯一入口. 示例1:获取组织机构对象 this.Engine.Organization.GetUnit("组织ID"); 示例 ...
- SNMP简单网络管理协议
声明:以下内容是学习谌玺老师视频整理出来(http://edu.51cto.com/course/course_id-861.html) SNMP(Simple Network Management ...