数据库表
引用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
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 学生姓名
/// </summary>
public string StuName { get; set; }
/// <summary>
/// 性别
/// </summary>
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<StudbContext> options)
: base(options)
{
}
public virtual DbSet<TbStu> 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<TbStu>(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();//保存对数据库的修改