EntityFramewrokCore 层构建
1.创建EntityFrameworkCore项目
2.引用
Volo.Abp.AspNetCore //ABP核心
Volo.Abp.EntityFrameworkCore //EFCore支持性
3.创建Module文件
继承AbpModule,依赖AbpEntityFrameworkCoreModule
[DependsOn(typeof(Volo.Abp.EntityFrameworkCore.AbpEntityFrameworkCoreModule))]
public class AbpEntityFrameworkCoreModule:AbpModule
{
}
4.创建数据库Context上下文类继承AbpDbContext
public class AbpEntityDBContext : AbpDbContext<AbpEntityDBContext>
{
public AbpEntityDBContext(DbContextOptions<AbpEntityDBContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
5.EntityFramewrokCore模块里面注入DbContext
[DependsOn(typeof(Volo.Abp.EntityFrameworkCore.AbpEntityFrameworkCoreModule))]
public class AbpEntityFrameworkCoreModule:AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
//注入DB
context.Services.AddAbpDbContext<AbpEntityDBContext>();
//使用sqlserver数据库
Configure<AbpDbContextOptions>(opt => opt.UseSqlServer());
}
}
6.在Web主程序appsettings.json里面配置连接字符串
7.在数据库上下文类加入特性
[ConnectionStringName("Default")]
完成连接字符串配置
8.配置主模块加入依赖
typeof(AbpDemoEntityFrameworkCoreModule)
扫描二维码,在手机上阅读!
评论