As a model for automatically increasing data type

Hello, I am trying to define a CatergoryModel for my MVC3 application. And I would like to know how I can set Id for auto increase.

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

    }
+5
source share
3 answers

Just add the annotation [Key]before the Id field.

+7
source
using System.ComponentModel.DataAnnotations;
public class Category
{


   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Required]
    public int Id { get; set; }
}
+6
source

, , - , "", , . column identity in visual studio

Entity Framework Code First - v4. : Entity Framework 4 Code First , NHibernate?

If you are using 4.1 and you have an existing database, look at this link, pay attention to the support of existing identification fields:

Using Entity Framework 4.1 Code First with an existing database I'm not sure if this is supported for code first (without db) as early as 4.1 though.

+4
source

All Articles