MySQL基础知识学习——EntityFrameworkCore 使用(九)

数据库表

在这里插入图片描述

引用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();//保存对数据库的修改
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值