1.dotnet ef 无法执行,因为找不到指定的命令或文件
dotnet tool install --global dotnet-ef
2.dotnet 指定要使用的项目文件
删除项目文件中多余的 .csproj
3.Your startup project 'WebApplication1' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.
dotnet add package Microsoft.EntityFrameworkCore.Design
4.EntityFramework Core DB First
dotnet ef dbcontext scaffold "Data Source=127.0.0.1;uid=[用户名];pwd=[密码];Database=[数据库];Trusted_Connection=True;integrated security=false; TrustServerCertificate=true "Microsoft.EntityFrameworkCore.sqlServer -o Models
5.ORM框架连接配置化
"ConnectionStrings": {
"DefaultConnection": "Data Source=127.0.0.1;uid=sa;pwd=123456;Database=test;Trusted_Connection=True;integrated security=false; TrustServerCertificate=true"}
6.将TestDbContext交给IOC容器管理
builder.Services.AddDbContext<TestDbContext>(options =>
{
options.UseSqlServer("name=ConnectionStrings:DefaultConnection");
});
EF Core环境搭建
1.首先要创建C#对象,用以对应数据库中的表,该C#对象也成为实体类。
public class Book
{
//必须是属性
public long Id { get; set; }//主键
public string Title { get; set; }//标题
public DateTime PubTime { get; set; }//发布日期
public double Price { get; set; }//单价
public string AuthorName { get; set; }//作者名字
2.根据所用的数据库选择NuGet包,所以安装Microsoft.EntityFrameworkCore.SqlServer。
3.需要实现一个配置类,用来说明实体类和数据库表是怎么映射的,该配置类要实现IEnityTypeConfiguration
接口。
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;public class BookEntityConfig : IEntityTypeConfiguration<Book> //指定是对那个类进行配置
{
public void Configure(EntityTypeBuilder<Book> builder) //实现接口
{
builder.ToTable("T_Books"); //实体对象在数据库中表的名字是“T_Books”
//没有详细设置每个属性在数据库中的列名和数据类型
//会默认把属性名字作为列名,并根据属性类型来推断数据库中的数据类型
//可以根据需要修改实体类的配置,修改数据库的表
//HasMaxLength(50):最大长度为50 IsRequired():不可为空
builder.Property(e => e.Title).HasMaxLength(50).IsRequired();
builder.Property(e => e.AuthorName).HasMaxLength(20).IsRequired();
}
}
4.创建数据访问类,即上下文类。必须实现DbContext
接口。
using Microsoft.EntityFrameworkCore;
class TestDbContext : DbContext
{
public DbSet<Book> Books { get; set; } //可对上文的Book实体进行操作
//配置数据库的连接
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder..UseSqlServer("name=ConnectionStrings:DefaultConnection");
optionsBuilder.LogTo(Console.WriteLine);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//设置需要加载的程序集
//加载当前程序集中所有实现了IEntityTypeConfiguration接口的类
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
}
}
5. EF Core的环境搭建完成,总结一下,目前还没有数据库只是定义了实体对象, EF Core会根据我们定义的实体对象自动生成数据库,这种操作也被称之为迁移(migration)。
6. 目前只是实体对象创建完成,但是还没有在数据库中生成相应的表。安装Nuget包Microsoft.EntityFrameworkCore.Tools。
7. 在程序包管理器控制台中执行Add-Migration 自定义名称,这个一个迁移命令,建议名称是有意义的。会自动在项目下生成Migrations文件夹,文件夹下生成相应的代码,其中名称为日期ID号_自定义名称的类,其主要功能是创建数据库。
8. 以上只是创建了生成数据库的类,但并没有执行。在程序包管理器控制台中使用Update-database执行数据库迁移代码。
9. 如果此时修改了BookEntityConfig中属性设置,需要再次执行一次Add-Migration 自定义名称,此时名称要和之前的不一样。再次执行Update-database