ADO.NET Entity Framework : Working with Stored Procedure

Here we go, I generally get questions from developers that if they will have to scrub the existing powerful stored procedure they have written by investing time and money. The answer to that is “NO”. You must enjoy using it as it has its own power.

 

So it is very simple in ADO.NET Entity Framework. Little bit of background before we start,

 

We need a table

 

CREATE TABLE [dbo].[Emp](

      [Id] [int] IDENTITY(1,1) NOT NULL,

      [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

 CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED

(

      [Id] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

 

There will be three stored procedures, insert/update/delete

 

Insert

++++

ALTER PROCEDURE [dbo].[InsertEmp]  

(

            @Name VARCHAR(50)

)    

AS

BEGIN

      INSERT INTO Emp(Name) VALUES(@Name)

END  

 

Update

+++++

ALTER PROCEDURE [dbo].[UpdateEmp]

(

      @Id int,

      @Name VARCHAR(50)

)    

AS

      UPDATE Emp SET [Name] = @Name WHERE ID = @Id

      RETURN

 

Delete

++++

ALTER PROCEDURE [dbo].[DeleteEmp]

(

      @Id int

)

 

AS

      DELETE Emp WHERE ID = @Id

 

 

Now the story begins, you create your apps in Visual Studio 2008 (any kind). And add edmx to it. While creating the model select the stored procedures and table you will be working with.

 

So your final edmx would look like,

 

EDMX

 

 

 

 

Model Browser After that you right click on the edm designer and choose “Mapping Details”. In that you choose “Map Entity to Functions” then put the required stored procedures. You are done.

 

Stored Procedure mapping

Now if you are observing the SQL Profiler you would be able to see that the stored procedures are getting executed instead of your T-SQL.

 

I am planning to create a screen capture, when ready I will add that to this blog.

 

Namoskar!!!

转载于:https://www.cnblogs.com/JosephLiu/archive/2010/02/17/1668852.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值