通过Tom Dykstra的Getting Started with Entity Framework 6 Code First using MVC 5教程,part 9涵盖了如何设置EF6以使用CUD的存储过程。CUD的存储过程:脚手架插入存储过程中两个SELECT语句的用途是什么?
当DepartmentSP
迁移经由包管理控制台添加,以下CreateStoredProcedure()调用自动生成以创建Department_Insert存储过程:
CreateStoredProcedure(
"dbo.Department_Insert",
p => new
{
Name = p.String(maxLength: 50),
Budget = p.Decimal(precision: 19, scale: 4, storeType: "money"),
StartDate = p.DateTime(),
InstructorID = p.Int(),
},
body:
@"INSERT [dbo].[Department]([Name], [Budget], [StartDate], [InstructorID])
VALUES (@Name, @Budget, @StartDate, @InstructorID)
DECLARE @DepartmentID int
SELECT @DepartmentID = [DepartmentID]
FROM [dbo].[Department]
WHERE @@ROWCOUNT > 0 AND [DepartmentID] = scope_identity()
SELECT t0.[DepartmentID]
FROM [dbo].[Department] AS t0
WHERE @@ROWCOUNT > 0 AND t0.[DepartmentID] = @DepartmentID"
);
为什么有在自动生成的存储在两个SELECT
语句程序?
我测试了以下简化:
CreateStoredProcedure(
"dbo.Department_Insert",
p => new
{
Name = p.String(maxLength: 50),
Budget = p.Decimal(precision: 19, scale: 4, storeType: "money"),
StartDate = p.DateTime(),
InstructorID = p.Int(),
},
body:
@"INSERT [dbo].[Department]([Name], [Budget], [StartDate], [InstructorID])
VALUES (@Name, @Budget, @StartDate, @InstructorID)
SELECT t0.[DepartmentID]
FROM [dbo].[Department] AS t0
WHERE @@ROWCOUNT > 0 AND t0.[DepartmentID] = scope_identity()"
);
...这似乎很好地工作,但我可能失去了一些东西。我已阅读What's New in Entity Framework 6 (Plus How To Upgrade!)和Code First Insert/Update/Delete Stored Procedure Mapping spec。另外,我查看了EF6 git commit history,发现commit 1911dc7,这是迁移中启用存储过程脚手架的第一部分。