代码改变世界

ADO.NET Entity Framework : Working with Stored Procedure

  AnyKoro  阅读(453)  评论(0编辑  收藏  举报

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!!!

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示