This question already has answers here:
Entity Framework and SCOPE_IDENTITY
(4 answers)
Closed 5 years ago.
I am inserting records in database using stored procedures like below
db.Database.ExecuteSqlCommand("sp_insertNewRecord");
Now I want to get the Id of last inserted record.
Due to some reason I can't use below function
db.TableName.Add(record);
db.SaveChanges();
So please don't suggest me to use this.
Create an output variable in your stored procedure like
create procedure [dbo].[Procedurename] #returnVal int output
as
SET #returnVal = SCOPE_IDENTITY();
After inserting the record get the SCOPE_IDENTITY() and set that into an OUTPUT variable.
and get the id in entity framework while calling stored procedure.
Hope this help!
Related
This question already has answers here:
Entity Framework - stored procedure return value
(5 answers)
Closed 5 years ago.
The problem is that many Stored Procedure must be return status code.
Which marked as
RETURN #status
so the EF (6.x) didn't recognize return type and SP call returns modified rows count (e.g 1,2 etc.)
The workaround to replace RETURN to SELECT is impossible
Change EF call to ADO.NET IMPOSSIBLE
So there is any workaround to get right value from SP without modifying SP
I am guessing you are using database approach. From the designer you can specify the return type of the stored procedure. Refer to this question here for more details how to do that.
Keep in mind that you wont need return part, just put in there select #status
This question already has answers here:
Adding a column to all user tables in t-sql
(2 answers)
Closed 6 years ago.
I created a C# app with a SQL Server 2014 backend. I was only intending to use the app for myself but now I have interest from other users. I never had the forethought to add a user id to any of the tables and my question is how could generate a script to add a uniqueid column to all the tables. I don't want to create a new database for every user and would prefer that every table has a unique column for the user.
Try this :
exec sp_msforeachtable 'alter table ? add new_guid uniqueidentifier null';
This question already has answers here:
Entity Framework - default values doesn't set in sql server table
(4 answers)
Closed 8 years ago.
I have a table with a uniqueidentifier column.
What is the Default Value or Binding i should set in order for each new row to generate a new uniqueidentifier?
Setting the default to newid() always returns 00000000-0000-0000-0000-000000000000
EDIT:
Apparently i got it all wrong, the insert was done from Entity Framework, and it doesn't handle this scenario.
Entity Framework - default values doesn't set in sql server table
newid() should work.
See images below:
You have another issue there.
This question already has answers here:
using stored procedure in entity framework
(6 answers)
Closed 9 years ago.
There is a stored procedure that executes exec command and returns some columns. Columns and number of them are varied. How can I call this stored procedure from Entity Framework?
You have to create a ComplexType in the EntityFramework and then call the stored procedure:
public ObjectResult<yourComplextype> MyMethod()
{
return _dbContext.usp_Yourprocedure();
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Invoking CLR stored procedures
Does anybody know how to call a CLR stored procedure using select statement?
As far as I know it's not possible without OPENQUERY or OPENROWSET, you'd better use CLR table function.
If you need to insert data from stored procedure
insert into Table
exec <your CLR procedure>