如何使用存储过程获取Entity Framework中的标识?

时间:2020-12-04 02:19:34

I am using Entity Framework 5 database first approach. Now I am inserting data into the table and again returning the IDENTITY.

我正在使用Entity Framework 5数据库的第一种方法。现在我将数据插入表中并再次返回IDENTITY。

ALTER PROCEDURE [dbo].[spAdmissonSlip_Insert]
(
    @paramStdClassID int,
    @paramFeeMonth varchar(20),
    @paramAdmissionFee int = null,
    @paramTutionFee int,
    @paramFeeSlip int = null,
    @paramResultFile int = null,
    @paramExamFee int = null,
    @paramSciCompFee int = null,
    @paramExtra int  =null
)
AS
BEGIN
    insert into tblFee 
    values (@paramStdClassID, @paramFeeMonth, NULL, @paramAdmissionFee,
            @paramTutionFee, @paramFeeSlip, @paramResultFile,
            @paramExamFee, @paramSciCompFee, @paramExtra, 0);

    select @@IDENTITY;
END

My question is how to get this IDENTITY on front-end. Whether I should use the output parameter OR there is any different method to get this in integer type variable.

我的问题是如何在前端获得这个IDENTITY。我是否应该使用输出参数或者有任何不同的方法来获取整数类型变量。

Here is the calling statement.

这是调用语句。

int FeeID = Convert.ToInt32 (OE.spAdmissonSlip_Insert(obj.StdClassID, obj.FeeMonth, obj.AdmissionFee, obj.TutionFee,
                            obj.FeeSlip, obj.ResultFile, obj.ExamFee, obj.SciCompFee, obj.Extra));

And this is the method definition..

这是方法定义..

 public ObjectResult<spAdmissonSlip_Insert_Result> spAdmissonSlip_Insert(Nullable<global::System.Int32> paramStdClassID, global::System.String paramFeeMonth, Nullable<global::System.Int32> paramAdmissionFee, Nullable<global::System.Int32> paramTutionFee, Nullable<global::System.Int32> paramFeeSlip, Nullable<global::System.Int32> paramResultFile, Nullable<global::System.Int32> paramExamFee, Nullable<global::System.Int32> paramSciCompFee, Nullable<global::System.Int32> paramExtra)
{
    ...
}

PS: I'm new to EF.

PS:我是EF的新手。

1 个解决方案

#1


0  

Answering your question about using output parameters or other method, I use Scope_Identity() in my applications and it works fine.

回答关于使用输出参数或其他方法的问题,我在我的应用程序中使用Scope_Identity()并且它工作正常。

Change the line select @@IDENTITY; for:

更改行选择@@ IDENTITY;对于:

SELECT Scope_Identity() AS Id

Check out the answer in this thread: How to obtain the identity of an entity after calling SaveChanges() when the entity is mapped to stored procedures

查看此主题中的答案:当实体映射到存储过程时,如何在调用SaveChanges()后获取实体的标识

Let me know if you have any issues with this approch.

如果您对此approch有任何问题,请告诉我。

#1


0  

Answering your question about using output parameters or other method, I use Scope_Identity() in my applications and it works fine.

回答关于使用输出参数或其他方法的问题,我在我的应用程序中使用Scope_Identity()并且它工作正常。

Change the line select @@IDENTITY; for:

更改行选择@@ IDENTITY;对于:

SELECT Scope_Identity() AS Id

Check out the answer in this thread: How to obtain the identity of an entity after calling SaveChanges() when the entity is mapped to stored procedures

查看此主题中的答案:当实体映射到存储过程时,如何在调用SaveChanges()后获取实体的标识

Let me know if you have any issues with this approch.

如果您对此approch有任何问题,请告诉我。