Entity Framework 6 Recipes 2nd Edition(10-10)译 - > 为TPH继承的插入、更新、删除操作映射到存储过程
10-10. 为TPH继承的插入、更新、删除操作映射到存储过程
问题
TPH继承模型,想把它的插入、修改、删除操作映射到存储过程
Solution
假设数据库有一个描述不同种类的产品表(Product )(见Figure 10-13). 而且为这个表的每种产品创建了创建了派生模型,这个模型如Figure 10-14.

Figure 10-13. 一个含有鉴别列(ProductType)的产品表, 表的每行按该列的值划分不同的产品

Figure 10-14. TPH继承形式的模型
接下来把这个模型的插入、更新、删除操作映射到存储过程:
1. 在数据库里,创建 Listing 10-26 所示的存储过程. 这些存储过程为Book 和 DVD 实体处理插入、更新、删除操作。
Listing 10-26. The Stored Procedure We Map to the Insert, Update, and Delete Actions for the Model
create procedure [chapter10].[InsertBook](@Title varchar(50), @Publisher varchar(50))
as
begin
insert into Chapter10.Product (Title, Publisher, ProductType) values(@Title,@Publisher, 'Book')
select SCOPE_IDENTITY() as ProductId
end
go
create procedure [chapter10].[UpdateBook](@Title varchar(50), @Publisher varchar(50), @ProductId int)
as
begin
update Chapter10.Product set Title = @Title, Publisher = @Publisher where ProductId = @ProductId
end
go
create procedure [chapter10].[DeleteBook](@ProductId int)
as
begin
delete from Chapter10.Product where ProductId = @ProductId
end
go
create procedure [chapter10].[InsertDVD](@Title varchar(50), @Rating varchar(50))
as
begin
insert into Chapter10.Product (Title, Rating, ProductType) values(@Title, @Rating, 'DVD')
select SCOPE_IDENTITY() as ProductId
end
go
create procedure [chapter10].[DeleteDVD](@ProductId int)
as
begin
delete from Chapter10.Product where ProductId = @ProductId
end
go
create procedure [chapter10].[UpdateDVD](@Title varchar(50), @Rating varchar(50), @ProductId int)
as
begin
update Chapter10.Product set Title = @Title, Rating = @Rating where ProductId = @ProductId
end
2.右击模型的设计视图,选择“从数据库更新模型. 选择新建的存储过程, 单击“完成”,完成更新.
3.右击 Book 实体,选择“存储过程映射”.映射 InsertBook,UpdateBook, 和DeleteBook 存储过程到相应的操作。为插入操作绑定ProductId列 (见 Figure 10-15).
Figure 10-15. 映射存在过程到Book实体的插入、更新、删除操作. 特别注意要把插入操作绑定结果列绑定到ProductId.
4. 右击 DVD 实体,选择“存储过程映射”, 映射 InsertBook,UpdateBook, 和DeleteBook 存储过程到相应的操作。为插入操作绑定ProductId列(见 Figure 10-16).
它是如何工作的?
我们为Book和DVD实体的插入、更新、删除操作创建了存储过程,并且引入到模型. 引入后,我们把它们分别映射到相应的实体的相应操作上,需要注意的是两个实体的结果列绑定都需要绑定ProductId属性,这样就可以确保存储过程返回的产品自动创建的ProductId列的值映射到实体的ProductId属性上。
TPH继承可以通过执行插入的存储过程,把ProductType值插入到表中, EF能根据ProductType值,正确地实体化出派生实体.
接下来的Listing 10-27 代码演示了插入、更新、删除和查询.
Listing 10-27. Exercising the Insert, Update, and Delete Actions
class Program
{
static void Main(string[] args)
{
using (var context = new EFRecipesEntities1010())
{
var book1 = new Book
{
Title = "A Day in the Life",
Publisher = "Colorful Press"
};
var book2 = new Book
{
Title = "Spring in October",
Publisher = "AnimalCover Press"
};
var dvd1 = new DVD { Title = "Saving Sergeant Pepper", Rating = "G" };
var dvd2 = new DVD { Title = "Around The Block", Rating = "PG-13" };
context.Products.Add(book1);
context.Products.Add(book2);
context.Products.Add(dvd1);
context.Products.Add(dvd2);
context.SaveChanges();
// update a book and delete a dvd
book1.Title = "A Day in the Life of Sergeant Pepper";
context.Products.Remove(dvd2);
context.SaveChanges();
}
using (var context = new EFRecipesEntities1010())
{
Console.WriteLine("All Products");
Console.WriteLine("============");
foreach (var product in context.Products)
{
if (product is Book)
Console.WriteLine("'{0}' published by {1}",
product.Title, ((Book)product).Publisher);
else if (product is DVD)
Console.WriteLine("'{0}' is rated {1}",
product.Title, ((DVD)product).Rating);
}
}
Console.WriteLine("\npress any key to exit...");
Console.ReadKey();
}
}
输出结果如下面的 Listing 10-27所示:
All Products
============
'Spring in October' published by AnimalCover Press
'A Day in the Life of Sergeant Pepper' published by Colorful Press
'Saving Sergeant Pepper' is rated G
Entity Framework 6 Recipes 2nd Edition(10-10)译 - > 为TPH继承的插入、更新、删除操作映射到存储过程的更多相关文章
- Entity Framework 6 Recipes 2nd Edition 译 -> 目录 -持续更新
因为看了<Entity Framework 6 Recipes 2nd Edition>这本书前面8章的翻译,感谢china_fucan. 从第九章开始,我是边看边译的,没有通读,加之英语 ...
- Entity Framework 6 Recipes 2nd Edition(9-1)译->用Web Api更新单独分离的实体
第九章 在N层结构的应用程序中使用EF 不是所有的应用都能完全地写入到一个单个的过程中(就是驻留在一个单一的物理层中),实际上,在当今不断发展的网络世界,大量的应用程序的结构包含经典的表现层,应用程, ...
- 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(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(支付 ...
- Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include
问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...
- Entity Framework 6 Recipes 2nd Edition(目录索引)
Chapter01. Getting Started with Entity Framework / 实体框架入门 1-1. A Brief Tour of the Entity Framework ...
随机推荐
- 再讲IQueryable<T>,揭开表达式树的神秘面纱
接上篇<先说IEnumerable,我们每天用的foreach你真的懂它吗?> 最近园子里定制自己的orm那是一个风生水起,感觉不整个自己的orm都不好意思继续混博客园了(开个玩笑).那么 ...
- Entity Framework 6 Recipes 2nd Edition 译 -> 目录 -持续更新
因为看了<Entity Framework 6 Recipes 2nd Edition>这本书前面8章的翻译,感谢china_fucan. 从第九章开始,我是边看边译的,没有通读,加之英语 ...
- Modify Branding of FreeCAD
Modify Branding of FreeCAD eryar@163.com This article describes the Branding of FreeCAD. Branding me ...
- ASP.NET Core中如影随形的”依赖注入”[上]: 从两个不同的ServiceProvider说起
我们一致在说 ASP.NET Core广泛地使用到了依赖注入,通过前面两个系列的介绍,相信读者朋友已经体会到了这一点.由于前面两章已经涵盖了依赖注入在管道构建过程中以及管道在处理请求过程的应用,但是内 ...
- 【原创分享·微信支付】 C# MVC 微信支付教程系列之扫码支付
微信支付教程系列之扫码支付 今天,我们来一起探讨一下这个微信扫码支付.何为扫码支付呢?这里面,扫的码就是二维码了,就是我们经常扫一扫的那种二维码图片,例如,我们自己添 ...
- 算法与数据结构(十六) 快速排序(Swift 3.0版)
上篇博客我们主要聊了比较高效的归并排序算法,本篇博客我们就来介绍另一种高效的排序算法:快速排序.快速排序的思想与归并排序类似,都是采用分而治之的方式进行排序的.快速排序的思想主要是取出无序序列中第一个 ...
- jQuery.template.js 简单使用
之前看了一篇文章<我们为什么要尝试前后端分离>,深有同感,并有了下面的评论: 我最近也和前端同事在讨论这个问题,比如有时候前端写好页面给后端了,然后后端把这些页面拆分成很多的 views, ...
- 解决Android Studio 无法显示Layout视图问题
在Android Studio 当中,如果你选择的SDK的版本 与你所显示的视图版本不一致时,会出现这个错误 Exception raised during rendering:com/android ...
- ASP.NET MVC——模型绑定
这篇文章我们来讲讲模型绑定(Model Binding),其实在初步了解ASP.NET MVC之后,大家可能都会产生一个疑问,为什么URL片段最后会转换为例如int型或者其他类型的参数呢?这里就不得不 ...
- 用Kotlin创建第一个Android项目(KAD 01)
原文标题:Create your first Android project using Kotlin (KAD 01) 作者:Antonio Leiva 时间:Nov 21, 2016 原文链接:h ...