ORM(Object-Relational-Mapping)
- EF core (稳定团队用),学习成本高- SQLServer完美支持- MySQL和PostgreSQL 有小坑,也可以解决
- Dapper (团队不稳定),低学习成本
开发环境搭建
- 新建业务类
- 建立配置类
- 建立DbContext类
- 迁移生成数据库
- 安装:
Install-Package Microsoft.EntityFrameworkCore -Version 5.0.17
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 5.0.17
Install-Package Microsoft.EntityFrameworkCore.Tools -Version 5.0.17- 踩坑: 如果安装的版本不兼容回滚的时候,调用方法的时候会出现各种无法识别的问题- 比如: builder.ToTable("T_Books"); // ToTable()方法一直无法被识别
// Book.cs......
namespace ConsoleAppORM
{class Book{public long Id { get; set; }public string Title { get; set; }public DateTime PubTime { get; set; }public double Price { get; set; }}
}// Person.cs......
namespace ConsoleAppORM
{public class Person{public long Id { get; set; }public string Name { get; set; }public int Age { get; set; }}
}
// BookConfig.csusing Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;namespace ConsoleAppORM
{class BookConfig : IEntityTypeConfiguration<Book>{public void Configure(EntityTypeBuilder<Book> builder){ builder.ToTable("T_Books");}}
}// PersonConfig.csusing Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;namespace ConsoleAppORM
{public class PersonConfig : IEntityTypeConfiguration<Person>{public void Configure(EntityTypeBuilder<Person> builder){builder.ToTable("T_Persons");}}
}
using Microsoft.EntityFrameworkCore;namespace ConsoleAppORM
{class MyDbContext:DbContext{public DbSet<Book> Books { get; set; }public DbSet<Person> Persons { get; set; }protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){base.OnConfiguring(optionsBuilder);string connStr = "Server=.;Database=demo1;Trusted_Connection=True;MultipleActiveResultSets=true";optionsBuilder.UseSqlServer(connStr);}protected override void OnModelCreating(ModelBuilder modelBuilder){base.OnModelCreating(modelBuilder);modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);}}
}
PM> ADD-Migration InitialCreate
Build started...
Build succeeded.PM> Update-Database
Build started...
Build succeeded.
......
Done.
- 新建连接
- 服务器名称 填 . 或 (local) 或 localhost
- 身份验证 选 Windows 身份验证

- 如果表的结构有变更,需重新创建迁移记录并更新数据库
......
namespace ConsoleAppORM
{public class Person{......public int Age { get; set; }// 新增字段public string BirthPlace { get; set; }}
}- 创建迁移记录并刷新数据库// 给予此次的迁移记录取一个名字
PM> Add-Migration AddPersonBirthPlace
......
PM> Update-Database
......
Done.
PM>
// Person.cs......namespace ConsoleAppORM
{public class Person{......public string BirthPlace { get; set; }# 新增public double? Salary { get; set; }}
}// Book.cs
......namespace ConsoleAppORM
{class Book{......# 新增public string AuthorName { get; set; }}
}// BookConfig.csusing Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;namespace ConsoleAppORM
{class BookConfig : IEntityTypeConfiguration<Book>{public void Configure(EntityTypeBuilder<Book> builder){builder.ToTable("T_Books");# 新增对字段的限制builder.Property(b => b.Title).HasMaxLength(50).IsRequired();builder.Property(b => b.AuthorName).HasMaxLength(20).IsRequired();}}
}// Dog.cs 新创建
......
namespace ConsoleAppORM
{public class Dog{public long Id { get; set; }public string Name { get; set; }public string Color { get; set; }}
}// DogConfig.cs 新创建
......
namespace ConsoleAppORM
{public class DogConfig : IEntityTypeConfiguration<Dog>{public void Configure(EntityTypeBuilder<Dog> builder){builder.ToTable("T_Dogs");}}
}// MyDbContext.csusing Microsoft.EntityFrameworkCore;namespace ConsoleAppORM
{class MyDbContext:DbContext{......# 注册public DbSet<Dog> Dogs { get; set; }protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){......}protected override void OnModelCreating(ModelBuilder modelBuilder){......}}
}
- 创建迁移记录并迁移(如果报错则不成功,根据异常来修复)