Table not incrementing after an insert in asp.net mvc

前端 未结 1 1192
别跟我提以往
别跟我提以往 2021-01-29 04:21

I\'m working on an ASP.NET MVC project. I have created the various models and the viewmodels to use in my project. I have also seeded my database table with seed data but upon i

相关标签:
1条回答
  • 2021-01-29 05:01

    Form what I understand, you need to make ID as auto incremented value.

    public class User
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string RefIndicator { get; set; }
        public Team TeamCategory { get; set; }
        public byte TeamId { get; set; }
        public bool IsRegistered { get; set; }
        public DateTime DateRegistered { get; set; }
        public DateTime? LastModified { get; set; }
        public UserRoles UserRoles { get; set; }
        public byte UserRolesId { get; set; }
    }
    

    I don't think it will work with byte, but you can use int for sure.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {        
        modelBuilder.Entity<User>().Property(a => a.Id).HasKey(b => b.Id);
        modelBuilder.Entity<User>().Property(a => a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
    
    0 讨论(0)
提交回复
热议问题