As already answered in the mentioned question, you can use the SEQUENCE Sql Server to get sequential code without repetition.
You can create it in Sql Server with the following command:
CREATE SEQUENCE dbo.NomeSequence
START WITH 1
INCREMENT BY 1 ;
To run this command using Entityframework Code-First, you need to create a Migration file. To create Migration follow the steps below:
Run this command in the Package Manager Console, in the layer of your application where the Entityframework Context:
Add-Migration RunSqlScript
It will create a Migrations folder, with a Runsqlscript.Cs file inside, this file has an up and a down method, in the up method put the script you want to run in the bd.
public partial class RunSqlScript : DbMigration
{
public override void Up()
{
Sql("CREATE SEQUENCE dbo.NomeSequence START WITH 1 INCREMENT BY 1");
}
public override void Down()
{
}
}
Do this by running the Package Manager Console command
Update-Database
With this in ASP.NET, when the user opens the screen of new registration, you can run in Sqlserver the command NEXT VALUE FOR to get the next sequential number.
Example:
SELECT NEXT VALUE FOR dbo.NomeSequence
To get sequense in Entityframework create a method to run select and return, this method can be in your Context or in the Repository of some model.
Example:
public int GetNextSequenceValue()
{
var rawQuery = Database.SqlQuery<int>("SELECT NEXT VALUE FOR dbo.NomeSequence;");
var task = rawQuery.SingleAsync();
int nextVal = task.Result;
return nextVal;
}
This way when the user opens the new registration screen, you call the Entity to pick up the sequence and then you can put it in the model code field, and you will have a sequential number reserved for it, even before entering the registration data in the bd.
Conclusion to your doubts:
Question 1: Is it possible to do what I am proposing?
Yes, it is possible to do, according to the example I sited.
Question 2: If I click add new, and then cancel, I suppose I created Cód: 0003, will that Cód still be available? or will jump from 0002 to 0004.
As you will get the code before persisting, if the user cancels the operation that sequence will be skipped, as in the example from 0002 to 0004. This needs to be so because there can be multiple users registering simultaneously, in which a user opens the new registration a sequential code already has to be reserved for him, so that the next user takes another code, thus maintaining the integrity of the sequence.
Man, I’m sorry, I’m a little layman.. research how to do this, I found this question .link . am using Codefirst. how do I map this to my client? I have a property on customer call
Codigo
.– Rafael Passos
@Rafaelpassos do not know exactly how this the structure of the project, but I changed the question how it would be more or less to do using Entityframework Code-First.
– Alisson Marqui
I managed to implement, but by the time you get to the
int nexVal = task.Result;
of the following error:InvalidOperationException: The specified cast from a materialized 'System.Int64' type to the 'System.Int32' type is not valid.
– Rafael Passos
solved using this script here.
SqlParameter result = new SqlParameter("@result", System.Data.SqlDbType.Int)
 {
 Direction = System.Data.ParameterDirection.Output
 };

 Database.ExecuteSqlCommand(
 "SELECT @result = (NEXT VALUE FOR MySequence)", result);

 return (int)result.Value;
– Rafael Passos