EF Core Reverse Engineering

database preparation

Create a new library students in the database, and then create the following table, the demonstration here uses MySQL

CREATE TABLE `student` (
  `id` int NOT NULL,
  `class` varchar(255) DEFAULT NULL COMMENT '班级',
  `name` varchar(255) DEFAULT NULL COMMENT '姓名',
  `age` int DEFAULT NULL COMMENT '年龄',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

 project

Create a new empty project and install the following three nuget packages

 Open the terminal, go to the current project path, and enter the following command

 dotnet ef dbcontext scaffold "server=127.0.0.1;uid=root;pwd=123456;database=students" Pomelo.EntityFrameworkCore.MySql

These two classes are automatically created in the project

 public partial class Student
    {
        public int Id { get; set; }
        /// <summary>
        /// 班级
        /// </summary>
        public string? Class { get; set; }
        /// <summary>
        /// 姓名
        /// </summary>
        public string? Name { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int? Age { get; set; }
    }

    public partial class studentsContext : DbContext
    {
        public studentsContext()
        {
        }

        public studentsContext(DbContextOptions<studentsContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Student> Students { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#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=127.0.0.1;uid=root;pwd=123456;database=students", Microsoft.EntityFrameworkCore.ServerVersion.Parse("8.0.33-mysql"));
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.UseCollation("utf8mb4_0900_ai_ci")
                .HasCharSet("utf8mb4");

            modelBuilder.Entity<Student>(entity =>
            {
                entity.ToTable("student");

                entity.Property(e => e.Id)
                    .ValueGeneratedNever()
                    .HasColumnName("id");

                entity.Property(e => e.Age)
                    .HasColumnName("age")
                    .HasComment("年龄");

                entity.Property(e => e.Class)
                    .HasMaxLength(255)
                    .HasColumnName("class")
                    .HasComment("班级");

                entity.Property(e => e.Name)
                    .HasMaxLength(255)
                    .HasColumnName("name")
                    .HasComment("姓名");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }

Then you can query the data in the database

By default, the generated classes are placed in the root directory, so it needs to be optimized. Add --context-dir and --output-dir after the command just now to specify the storage folder

dotnet ef dbcontext scaffold "server=127.0.0.1;uid=root;pwd=123456;database=students" Pomelo.EntityFrameworkCore.MySql --context-dir Data --output-dir Entities

 

Guess you like

Origin blog.csdn.net/weixin_65243968/article/details/130545816