Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need Next Auto Increment Value ... i am using Mysql Database... I need Linq query examples

What I have tried:

SQL
SELECT `AUTO_INCREMENT` FROM  INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'distributorempanel_new' AND   TABLE_NAME   = 'temp_rate_objective_new')
... this query mysql working fine.... but i need Linq query....
Posted
Updated 4-Sep-18 10:56am
v3

Don't even try. That's a dangerous game: MySQL is a multiuser environment, so you cannot in any way guarantee that the "next value" you fetch will be available when you want to use it as a different user may have taken it as well. And the risks of using the wrong unique value are horrible: sorting out a database once it's been coirrupted like that is a very nasty and difficult manual job.
Only ever fetch an ID once it's been used and assigned to a row - never try to get it in advance, it will only ever give you problems.
 
Share this answer
 
Comments
arul077 8-Dec-16 5:07am    
what is equalient in scope_identity() for Linq...?
Dave Kreskowiak 17-Dec-16 19:24pm    
There is no such thing.

The proper way to do this is to create a new record with at least the minimum data needed to save the record to the table, then you get back the id number automatically in the EF object you saved.
Maciej Los 17-Dec-16 18:11pm    
5ed!
Jon McKee 17-Dec-16 18:22pm    
Agree with everything Griff said :thumbsup:
/* Begin Stored Procedure */
USE [MyDatabase]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[My_StoredProcedure]
@VariableNum [int] = NULL,
@VariableDate [datetime] = NULL,
...
...
@Variable_Return_Id [int] OUTPUT
WITH EXECUTE AS CALLER
AS
Begin
INSERT INTO
My_Table(Var_Num ,
Var_Date ,
... ,
...

) VALUES (@VariableNum ,
@VariableDate
....
....
)
SET @Variable_Return_Id = @@IDENTITY
End
/* End Stored Procedure */

Note : You must create this stored procedure in SQL Server data table and add Visual studio project Linq dbml in My_StoredProcedure with drop down.
When you call is My_StoredProcedure add a new record and Return IDENTITY Number.

C# Example:
int AddRec()
{
int num=1;
DateTime DateVal = DateTime.Now.Date;
int ResultId;
ResultId=My_StoredProcedure(num, DateVal);
return(ResultId);
}

VB Example:
Function AddRec() as integer
Dim num As Integer = 1
Dim DateVal As DateTime = DateTime.Now.Date
Dim ResultId As integer
ResultId=My_StoredProcedure(num, DateVal)
AddRec=ResultId ' or Return ResultId
End Function
 
Share this answer
 
If using LINQ, then to get new value assigned to the Identity column is very simple. You don't need these many Stored Procedures to be invoked. You already have the object instance to be added, then simply read the Id property of this object after DB. SaveChanges() is invoked.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900