首页 > 解决方案 > 无法使用asp .net核心中的abp框架连接到sql数据库

问题描述

我正在使用 aspnetboilerplate 在 asp .net 核心中使用 DDD 架构的 web api 项目工作,该过程就像示例(https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core),但它没有工作(dbContextOptions.IsConfigured = false)

连接异常:

“值不能为空。参数名称:unitOfWork”

层级:

public IServiceProvider ConfigureServices(IServiceCollection services)
    {
       services.AddAbpDbContext<SampleDBContext>(options =>
    {
       DBContextOptionsConfigurer.Configure(options.DbContextOptions,    options.ConnectionString);
});
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        return services.AddAbp<ApiModule>();
    }

DBContextOptionsConfigurer 类:

public static void Configure(DbContextOptionsBuilder<SampleDBContext> dbContextOptions,string connectionString)
    {
    //dbContextOptions.IsConfigured = false,ConnectionStrings is in appsettings.json
        dbContextOptions.UseSqlServer(connectionString);
    }

上下文类:

public class SampleDBContext : AbpDbContext
{
    public DbSet<Language> Language { get; set; }
    public DbSet<NewsLetter> NewsLetter { get; set; }
    public DbSet<Post> Post { get; set; }
    public DbSet<PostCategory> PostCategory { get; set; }

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

Api 模块类:

[DependsOn(typeof(ApplicationModule),
    typeof(EntityFrameworkCoreModule),
    typeof(AbpAspNetCoreModule))]
public class ApiModule : AbpModule
{
    private readonly IConfigurationRoot _appConfiguration;

    public ApiModule(IHostingEnvironment env)
    {
        _appConfiguration = AppSetting.Get(env.ContentRootPath, env.EnvironmentName);
    }
    public override void PreInitialize()
    {
        Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString(Keys.ConnectionStringName);
        Configuration.Modules.AbpAspNetCore().CreateControllersForAppServices(typeof(ApplicationModule).Assembly, moduleName: "app", useConventionalHttpVerbs: true);
    }
    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(typeof(ApiModule).GetAssembly());
    }
}

如果您能在这方面帮助我,我将不胜感激。哪一步错了?

标签: c#sql-serverasp.net-coredatabase-connectionaspnetboilerplate

解决方案


推荐阅读