How to generate Identity value( from code behind ) in the database when a row is inserted in Asp.Net MVC ?

Goud.Kv
Posted by Goud.Kv under ASP.NET MVC category on | Points: 40 | Views : 1203
Below code snippet describes the generation of Identity value in the database from code behind in MVC.

Just modify your property in the Model as below,

public class myModel {
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int AutoId { get; set; }
}


'DatabaseGeneratedAttribute' is used to generate the Identity value automatically into the database.

'[Key]' mentioned above is used to define the primary key of the table.

Comments or Responses

Login to post response