---
title: "How to create a primary key in ASP.NET MVC 4 using code first approach"  
description: "How to create a primary key in ASP.NET MVC 4 using code first approach"  
author: "Anupam Mishra"  
published: 2016-02-02  
updated: 2016-02-02  
canonical: https://www.mindstick.com/forum/33949/how-to-create-a-primary-key-in-asp-dot-net-mvc-4-using-code-first-approach  
category: "c#"  
tags: ["c#", "asp.net", "asp.net mvc"]  
reading_time: 1 minute  

---

# How to create a primary key in ASP.NET MVC 4 using code first approach

Hi Everyone,I have creating a [model](https://yourviews.mindstick.com/view/81334/america-is-reopening-after-corona-lockdown-but-on-swedish-model) in [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc) using [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) first [approach](https://www.mindstick.com/interview/985/what-are-the-approaches-that-you-will-follow-for-making-a-program-very-efficient) but i [am unable](https://answers.mindstick.com/qa/95314/how-do-i-access-a-group-link-in-telegram-if-i-am-unable-to-access-the-link) to creating a [primary key](https://www.mindstick.com/blog/214/primary-key) in a model.Please can [anyone give me](https://answers.mindstick.com/qa/41194/can-anyone-give-me-some-high-and-low-points-of-each-of-the-four-first-presidential-administrations) a solution.Thank you.

## Replies

### Reply by Manoj Bhatt

Hi Anupam,For creating a [primary](https://www.mindstick.com/forum/33739/difference-between-primary-key-and-unique-key) key you used the DataAnnotations. Similar as follow:

```
  public class Students    {        [Key]        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]        public int Id { get; set; }
```


---

Original Source: https://www.mindstick.com/forum/33949/how-to-create-a-primary-key-in-asp-dot-net-mvc-4-using-code-first-approach

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
