Entity Framework 6 Recipes 2nd Edition(10-8)译 - >映射插入、修改、删除操作到存储过程

10-8. 映射插入、修改、删除操作到存储过程

问题

想要映射插入、修改、删除操作到存储过程

解决方案

假设已有运动员实体模型,如Figure 10-8所示. 对应的数据库表如Figure 10-9所示. 我们想要用存储过程来执行插入,修改,删除操作。

 

Figure 10-8. 运动员实体模型

 

Figure 10-9. 包含一些基本信息的运动员表

为实体映射存储过程到插入,修改,删除操作,执行以下操作:

1.在数据库里,创建如Listing 10-21所示的存储过程

Listing 10-21. The Stored Procedures for the Insert, Update, and Delete Actions

create procedure [chapter10].[InsertAthlete]

(@Name varchar(50), @Height int, @Weight int)

as

begin

insert into Chapter10.Athlete values (@Name, @Height, @Weight)

select SCOPE_IDENTITY() as AthleteId

end

go

 

create procedure [chapter10].[UpdateAthlete]

(@AthleteId int, @Name varchar(50), @Height int, @Weight int)

as

begin

update Chapter10.Athlete set Name = @Name, Height = @Height, [Weight] = @Weight

where AthleteId = @AthleteId

end

go

 

create procedure [chapter10].[DeleteAthlete]

(@AthleteId int)

as

begin

delete from Chapter10.Athlete where AthleteId = @AthleteId

end

2. 右击模型设计视图,选择“从数据库更新模型”,选择上面创建的三个存储过程,单击“完成”,这样存储过程就添加到了模型中

3. 右击Athlete实体, 选择“存储过程映射”,为每个操作选择对应的存储过程. 为插入操作的“结果列绑定” AthleteId (见Figure 10-10).

 

Figure 10-10. 映射存储过程,参数,和返回值 后的操作、修改、删除操作

它是如何工作的?

我们用存储过程更新了模型,这样模型就可以使用存储过程了,然后我们映射存储过程到实体的插入、修改、删除操作上.

在本小节, 我们尽可能让存储过程简单. 它们从实体属性里获取参数,并且执行操作.插入操作里我们需要返回存储过程返回自增列的ID值给实体.所以需要把存储过程返回的Id值映射到实体的AthleteId属性,这一步很重要.没有这一步,EF不能获取新创建的实体ID。

你可能会问“我什么时候把存储过程映射到实体操作了?”,其实大多数情况下,EF会自动为插入、修改、删除操作生成高效的代码。

你可能也会想知道:“那我什么时候需要用存储过程来映射操作?”,这个问题的答案有个最佳的实践:

•当你公司要求你用存储过程插入、修改、删除来操作表的时候。

• 你有一个额外的任务,需要执行额外的操作.比如, 你想要管理一个审计运算或执行一些复杂的业务逻辑, 又或是对一个用户进行权限安全检查等。

• 你的实体基于查询视图(QueryView) (见第6章和15章)

    static void Main(string[] args)

        {

            using (var context = new EFRecipesEntities1008())

            {

                context.Database.ExecuteSqlCommand("delete from chapter10.Athlete");

                context.Athletes.Add(new Athlete

                {

                    Name = "Nancy Steward",

                    Height = 167,

                    Weight = 53

                });

                context.Athletes.Add(new Athlete

                {

                    Name = "Rob Achers",

                    Height = 170,

                    Weight = 77

                });

                context.Athletes.Add(new Athlete

                {

                    Name = "Chuck Sanders",

                    Height = 171,

                    Weight = 82

                });

                context.Athletes.Add(new Athlete

                {

                    Name = "Nancy Rodgers",

                    Height = 166,

                    Weight = 59

                });

                context.SaveChanges();

            }

            using (var context = new EFRecipesEntities1008())

            {

                var all = context.Athletes;

                  context.Athletes.Remove(all.First(o => o.Name == "Nancy Steward"));

                all.First(o => o.Name == "Rob Achers").Weight = 80;

                context.SaveChanges();

            }

 

            using (var context = new EFRecipesEntities1008())

            {

                Console.WriteLine("All Athletes");

                Console.WriteLine("============");

                foreach (var athlete in context.Athletes)

                {

                    Console.WriteLine("{0} weighs {1} Kg and is {2} cm in height",

                    athlete.Name, athlete.Weight, athlete.Height);

                }

            }

            Console.WriteLine("\nPress any key to exit...");

            Console.ReadKey();

        }

输出结果如下Listing 10-22所示:


All Athletes

============

Rob Achers weighs 80 Kg and is 170 cm in height

Chuck Sanders weighs 82 Kg and is 171 cm in height

Nancy Rodgers weighs 59 Kg and is 166 cm in height


 

 

附:创建示例用到的数据库的脚本文件

 

 

posted @ 2016-01-22 22:10  kid1412  阅读(367)  评论(0编辑  收藏  举报