Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9087709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:45:28+00:00 2026-06-16T21:45:28+00:00

I do insertion with following in my ASP.NET MVC3 project: var query = INSERT

  • 0

I do insertion with following in my ASP.NET MVC3 project:

var query = "INSERT INTO MyTable VALUES(//some values)";
db.Database.ExecuteSqlCommand(query);

Is there an easy way to get the inserted row’s ID?

EDIT:

My stored procedure:

CREATE PROCEDURE addGetID
( 
    @PersonnelID INT,
    @ShiftWorkID BIGINT,
    @EntranceDateTime DATETIME,
    @WorkflowStateRelationID BIGINT
)
AS
INSERT INTO DynamicDataEntrances VALUES (@PersonnelID, @ShiftWorkID, @EntranceDateTime, @WorkflowStateRelationID)
SELECT @@IDENTITY

My code lines:

string dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
SqlParameter PersonnelID = new SqlParameter("@PersonnelID", 1);
SqlParameter ShiftWorkID = new SqlParameter("@ShiftWorkID", 2);
SqlParameter EntranceDateTime = new SqlParameter("@EntranceDateTime", dt);
SqlParameter WorkflowStateRelationID = new SqlParameter("@WorkflowStateRelationID ", db.WorkflowStateRelations.Where(wsr => wsr.WorkflowID == dp.WorkflowID).Select(x => x.ID).FirstOrDefault());
SqlParameter returnValue = new SqlParameter();
returnValue.Direction = ParameterDirection.ReturnValue;

// try 1
db.Database.ExecuteSqlCommand("addGetID(@PersonnelID, @ShiftWorkID, @EntranceDateTime, @WorkflowStateRelationID )", PersonnelID, ShiftWorkID, EntranceDateTime, WorkflowStateRelationID, returnValue);

// try 2
db.Database.ExecuteSqlCommand("addGetID @PersonnelID @ShiftWorkID @EntranceDateTime @WorkflowStateRelationID ", PersonnelID, ShiftWorkID, EntranceDateTime, WorkflowStateRelationID, returnValue);

// try 3
db.Database.ExecuteSqlCommand("exec addGetID @PersonnelID={0} @ShiftWorkID={1} @EntranceDateTime={2} @WorkflowStateRelationID={3} ", PersonnelID, ShiftWorkID, EntranceDateTime, WorkflowStateRelationID, returnValue);

var id = returnValue.Value;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:45:29+00:00Added an answer on June 16, 2026 at 9:45 pm

    I agree with [scartag][1] on this one. But to answer your question in 2 parts.

    If you’re going to use direct sql then the thing to do is create a stored proc that returns the @@IDENTITY value. Then you exec your Sp with a parameter of ParameterDirection.ReturnValue and use that value.

    Like such:

    SqlParameter entityField = new SqlParameter("@firstName", "Frank");
    SqlParameter returnValue = new SqlParameter();
    returnValue.Direction = ParameterDirection.ReturnValue;
    db.Database.ExecuteSqlCommand("sp_ByPassEFAndInsertEntity(@firstName)", 
                                  entityField, returnValue);
    var id = returnValue.Value;
    

    Or just use EF. Create and initialize your Entity, add it to the applicable DbSet and call savechanges. Your Entity instance’s Id column will be updated with the value generated by the database.

    EDIT 2: I applied the first solution to a local db of mine and could get it to give a return value either. After some research I was able to get a return value. I have a local db with a Blog table. I created an addBlog SP and this is how you can get a return value – but instead of using ExecSqlCommand you must use SqlQuery.

    Here’s my SP:

        ALTER PROCEDURE [dbo].[addBlog] 
            @BlogName varchar(50),
            @CreateDateTime datetime
        AS
        BEGIN
            insert into Blog (Name, DateTimeCreated) values (@BlogName, @CreateDateTime)
            declare @return int
            set @return = @@identity
            select @return
        END
    

    and my EF code:

        using (var db = new StackOverflowEntities()) {
            SqlParameter blogName = new SqlParameter("BlogName", "New Name 2");
            SqlParameter createDate = new SqlParameter("CreateDate", DateTime.Now);
    
            int? newIdentityValue = db.Database.SqlQuery<Int32>("addBlog @BlogName, @CreateDate", blogName, createDate).FirstOrDefault();
    
            Debug.WriteLine(newIdentityValue);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to implement a tagging system into my asp.net MVC project. When
In a ASP.NET MVC project I'm working on I have the following piece of
I've setup Castle Windsor in my ASP.NET Mvc 3 project and added the following
I'm using stored procedures and DataContext to insert data to SQL Server database (ASP.NET
I've cached a value using the ASP.NET Cache, with the following code: Cache.Insert(TEST_VALUE, 150,
Take the following article for example: http://weblogs.asp.net/psteele/archive/2009/11/23/use-dependency-injection-to-simplify-application-settings.aspx?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+dotnetmvp+%28Patrick+Steele%27s+.NET+Blog%29 I don't see what benefit there is
I have following code for inserting data into database using PDO. It inserts data
I'm following Pro ASP.Net MVC2 book and literally 90% of everything is new to
In my latest ASP.NET MVC 2 application I have been trying to put into
I'm working on a large Asp.Net MVC3 application (>50 views) and we are currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.