Search code examples
asp.net-mvc-3modelasp.net-mvc-scaffolding

How to Auto Increment Data Type Model


Hello I'm trying to define the CatergoryModel for my MVC3 app. And I would like to know how can I set Id to auto increment.

public class Category
    {
        [Required]
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        [Required]
        public string Type { get; set; }

        [Required]
        public string Title { get; set; }

        [Required]
        public string MetaKey { get; set; }

        [Required]
        public string MetaKeyDesc { get; set; }

        [Required]
        public DateTime CreatedAt { get; set; }

        [Required]
        public DateTime UpdatedAt { get; set; }

        public int CategoryId { get; set; }

    }

Solution

  • Simply put a [Key] annotation before the Id field.