c# - 无法使用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());
}
}
如果您能在这方面帮助我,我将不胜感激。哪一步错了?
解决方案
推荐阅读
- elixir - 如何处理凤凰频道的客户状态
- xamarin.forms - 带有 Xamarin 表单支持 QR 码的 Google 移动视觉 api
- java - 从 ArrayList 中检索随机对象:不兼容的类型
- android - Flutter --launch 过去工作时现在挂起。如何删除设备并重新创建?
- java - Java Spring表单验证器检查输入字符串是否包含特殊字符
- symfony - 为什么第一次加载 symfony 后停止工作?
- python - 如何摆脱附加到超类实例列表的子类中的 eval?
- android - 使用带有输入字符串值的 webview
- jenkins - gitlab-plugin : Jenkins Pipeline 和 Gitlab 之间的 openConnection
- ios - 使用 UICollectionViewDelegateFlowLayout 后约束未更新