ADO.NET Entity Framework : Working with Stored Procedure
2010-02-17 00:17
399 查看
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!!!
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!!!
相关文章推荐
- Using stored procedures with ADO.NET entity framework
- error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.
- How to execute a Stored Procedure with Entity Framework Code First
- Comparision Linq toSQL with ADO.net Entity Framework
- ADO.Net Entity Framework : (二) 實現 with(nolock) 查詢
- No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClie
- [转]Executing a stored procedure with an output parameter using Entity Framework
- No Entity Framework provider found for the ADO.NET provider with invariant
- Professional ADO.NET 3.5 with LINQ and the Entity Framework
- ADO.NET Entity Framework 学习(2)
- ASP.NET Identity with the Entity Framework
- Entity Framework (EF)/Linq To entity/ ESQL(entity sql)区别 ADO.NET Entity Framework:来自微软官方的ORM框架
- Microsoft SQL Server Compact 4.0&&ADO.NET Entity Framework 4.1&&MVC3
- linq to sql =>; ADO.NET Entity Framework
- 让ADO.NET Entity Framework支持Oracle数据库
- 一步一步体验 ADO.NET Entity Framework:第3步:理清思路——EDM中的关系和继承
- ADO.NET Entity Framework 试水——持久
- ADO.NET Entity Framework Extensions 示例应用
- Entity Framework Tutorial Basics(29):Stored Procedure in Entity Framework
- 热门的技术教程!ASP.NET MVC, JQuery, ASP.NET AJAX and ADO.NET Entity Framework