1. 程式人生 > 實用技巧 >Net Core + EF Core + Sql Server

Net Core + EF Core + Sql Server

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類(與資料庫互動)

  

  

public
class 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; } }
DemoContext

所引nuget包