7.3EF Core与ASP.NET Core集成
案例:
- Nuget安装Microsoft.EntityFrameworkCore.Relational、Microsoft.EntityFrameworkCore.Sqlite、Microsoft.EntityFrameworkCore.Tools
- 定义Book实体类
1 2 3 4 5 6
| public record Book { public Guid Id { get; set; } public string Name { get; set; } public double Price { get; set; } }
|
- 定义Book配置类
1 2 3 4 5 6 7
| public class BookConfig : IEntityTypeConfiguration<Book> { public void Configure(EntityTypeBuilder<Book> builder) { builder.ToTable("Books"); } }
|
- 增加上下文类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class MyDbContext:DbContext { public DbSet<Book> Books { get; set; } public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly); } }
|
- 在appsettings.json中增加数据库连接字符的设置
1
| "ConnectionStrings": { "Default": "Data Source=MySqLite.db" }
|
- 在Program.cs中使用依赖注入的方式对上下文的连接字符进行配置
1 2 3 4 5
| builder.Services.AddDbContext<MyDbContext>(opt => { string constr = builder.Configuration.GetConnectionString("Default"); opt.UseSqlite(constr); });
|
- 增加控制类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| [Route("[controller]/[action]")] public class TestController : ControllerBase { private readonly MyDbContext dbCtx; public TestController(MyDbContext dbCtx) { this.dbCtx = dbCtx; } [HttpPost] public async Task<IActionResult> Index() { dbCtx.Add(new Book { Id = Guid.NewGuid(), Name = "ddd", Price = 40 }); await dbCtx.SaveChangesAsync(); var book = dbCtx.Books.First(); return Content(book.ToString()); } }
|
- 如果在多项目环境下执行Add-Migration迁移命令的时候,迁移工具发生报错。此时可以使用
IDesignTimeDbContextFactory
接口来解决。当项目中存在一个该接口的时候,迁移工具会调用实现接口类的CreateDbContext方法来获取上下文对象,然后迁移工具使用这个上下文来连接数据库。
1 2 3 4 5 6 7 8 9 10 11 12
| class MyDesignTimeDbContextFactory:IDesignTimeDbContextFactory<MyDbContext> { public MyDbContext CreateDbContext(string[] args) { DbContextOptionsBuilder<MyDbContext> builder = new(); string connStr = Environment.GetEnvironmentVariable("ConnectionStrings:BooksEFCore"); builder.UseSqlServer(connStr); return new MyDbContext(builder.Options); } }
|
- 使用
Add-Migration Init、Update-database
等命令完成数据库的创建
上下文池
上下文被创建的时候,要执行实体类的配置,所以会消耗较多的资源,所以EF Core提供了AddDbContextPool
来注入上下文,对于使用了AddDbContextPool
注入的上下文,EF Core会优先从上下文池中获取实例。但是,因为池中的上下文实例会被反复使用,因此没有办法为上下文注入服务。
在项目开发时,建议使用“小上下文”策略,不要把项目中所有的实体放到一个上下文中,而是要将关联性大的实体放到一个上下文中。
如果采用“小上下文”策略,则需要手动注册所有的上下文,批量注册上下文的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public static IServiceCollection AddAllDbContexts(this IServiceCollection services, Action<DbContextOptionsBuilder> builder, IEnumerable<Assembly> assemblies) { Type[] types = new Type[] { typeof(IServiceCollection), typeof(Action<DbContextOptionsBuilder>), typeof(ServiceLifetime), typeof(ServiceLifetime) }; var methodAddDbContext = typeof(EntityFrameworkServiceCollectionExtensions).GetMethod("AddDbContext",1,types); foreach (var asmToLoad in assemblies) { foreach (var dbCtxType in asmToLoad.GetTypes().Where(t=>!t.IsAbstract && typeof(DbContext).IsAssignableFrom(t))) { var methodGenericAddDbContext = methodAddDbContext.MakeGenericMethod(dbCtxType); methodGenericAddDbContext.Invoke(null, new object[] { services, builder, ServiceLifetime.Scoped, ServiceLifetime.Scoped }); } } return services; }
|