在EF中,各個實體的插入、更新和刪除也都通過使用存儲過程來完成,以便提高點性能。這個類似於數據集。其步驟是:先定義存儲過程,然後在VS的可視化設計器,設置存儲過程映射即可。
下面,以為Supplier實體映射存儲過程為例。
分別建立插入、更新和刪除存儲過程。
InsertSuppliers存儲過程定義如下:
CREATE PROCEDURE [dbo].[InsertSuppliers]
-- Add the parameters for the stored procedure here
@CompanyName nvarchar(40),
@ContactName nvarchar(30),
@ContactTitle nvarchar(30),
@Address nvarchar(60),
@City nvarchar(15),
@Region nvarchar(15),
@PostalCode nvarchar(10),
@Country nvarchar(15),
@Phone nvarchar(24),
@Fax nvarchar(24),
@HomePage ntext
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
INSERT INTO Suppliers(CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax,HomePage)
VALUES(@CompanyName,@ContactName,@ContactTitle,@Address,@City,@Region,@PostalCode,@Country,@Phone,@Fax,@HomePage);
SELECT SCOPE_IDENTITY() AS SupplierID;
END