How to Auto Increment Data Type Model

Antarr Byrd picture Antarr Byrd · Aug 24, 2011 · Viewed 26k times · Source

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; }

    }

Answer

Eugene Pavlov picture Eugene Pavlov · Nov 29, 2011

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