EF 实体字段设置主键和自增

        [Key] //主键 
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  //设置自增
        public int id { get; set; }

        [ForeignKey("category")] //外键
        public int categoryid { get; set; }
        public Category category { get; set; }
原文地址:https://www.cnblogs.com/tiancai/p/4831667.html