MySQL基础知识学习

您所在的位置:网站首页 sqlserver基础知识 MySQL基础知识学习

MySQL基础知识学习

2023-05-20 19:18| 来源: 网络整理| 查看: 265

文章目录 数据库表引用EntityFrameworkCore运行运行结果TbStuStudbContext 查询新增删除

数据库表

在这里插入图片描述

引用EntityFrameworkCore

新建.netcore6 控制台程序 在这里插入图片描述

打开NuGet引用以下六个包 dotnet tool install --global dotnet-ef

在这里插入图片描述 在这里插入图片描述

运行

打开NugGet包管理器下的控制台运行

dotnet ef dbcontext scaffold "Server=localhost;User=root;Password=12345;Database=studb" "Pomelo.EntityFrameworkCore.MySql" 运行结果

根据数据库自动生成TbStu、StudbContext,浅看一下波er 在这里插入图片描述

TbStu public partial class TbStu { /// /// 主键 /// public int Id { get; set; } /// /// 学生姓名 /// public string StuName { get; set; } /// /// 性别 /// public string StuGender { get; set; } public int StuAge { get; set; } public DateTime? StuBirthday { get; set; } } StudbContext public partial class StudbContext : DbContext { public StudbContext() { } public StudbContext(DbContextOptions options) : base(options) { } public virtual DbSet TbStus { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) #warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263. => optionsBuilder.UseMySql("server=localhost;user=root;password=Yiqing7717;database=studb", Microsoft.EntityFrameworkCore.ServerVersion.Parse("8.0.32-mysql")); protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder .UseCollation("utf8mb4_0900_ai_ci") .HasCharSet("utf8mb4"); modelBuilder.Entity(entity => { entity.HasKey(e => e.Id).HasName("PRIMARY"); entity.ToTable("tb_stu"); entity.HasIndex(e => e.StuName, "stuName").IsUnique(); entity.Property(e => e.Id) .HasComment("主键") .HasColumnName("id"); entity.Property(e => e.StuBirthday).HasColumnType("datetime"); entity.Property(e => e.StuGender) .IsRequired() .HasMaxLength(1) .IsFixedLength() .HasComment("性别"); entity.Property(e => e.StuName) .IsRequired() .HasMaxLength(20) .HasComment("学生姓名"); }); OnModelCreatingPartial(modelBuilder); } partial void OnModelCreatingPartial(ModelBuilder modelBuilder); } 查询 StudbContext studbContext = new StudbContext(); { //查询所有 Console.WriteLine("查询所有"); var list = studbContext.TbStus.ToList(); foreach (var item in list) { Console.WriteLine($"{item.Id}---{item.StuName}---{item.StuBirthday}----{item.StuGender}"); } } { //查询结果--新建对象 Console.WriteLine("查询结果--新建对象"); var ls = studbContext.TbStus.Select(stu => new { name = stu.StuName, age = stu.StuAge }); foreach (var item in ls) { Console.WriteLine($"{item.name}---{item.age}"); } } 新增 { //新增 Console.WriteLine("新增"); studbContext.TbStus.Add(new TbStu { StuAge = 25, StuBirthday = DateTime.Now, StuGender = "男", StuName = "dfdbss" }); studbContext.SaveChangesAsync(); } 删除 //删除 Console.WriteLine("删除对象"); var stu = studbContext.TbStus.Where(stu=>stu.Id==25).FirstOrDefault(); studbContext.Remove(stu); studbContext.SaveChanges();//保存对数据库的修改


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3