Net Core + EF Core + Sql Server
阿新 • • 發佈:2020-07-21
1.新建空專案
2.建立實體類庫
建立實體
public class UsersInfo { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] //設定自增 public int SeqNum { get; set; } [Key,MaxLength(50)] public string UserID { get; set; } //[Required]必填 [Required,MaxLength(50)]實體public string UserName { get; set; } [MaxLength(1)] public int Gender { get; set; } [MaxLength(50)] public string BelongClass { get; set; } [MaxLength(255)] public string Disc { get; set; } }
3.再建立一個類庫專案
建立DemoContext類(與資料庫互動)
publicDemoContextclass DemoContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer( connectionString: "server=.;database=MyTestDB;uid=sjz2018dbuser;pwd=1ECD9E*#E9DCE1;" ); }public DbSet<ClassInfo> ClassInfos { get; set; } public DbSet<UsersInfo> UsersInfos { get; set; } }
所引nuget包