asp.net-core - 如何从不使用任何形式的 DI 的类库中检查环境
问题描述
当 DBA 创建数据库时,我们正在使用实体框架从数据库中构建类,我们不能先使用代码。
当脚手架创建一个 DbContext 类并使用硬编码到该类中的连接字符串填充 onconfiguring 方法时,我们有一个部分类覆盖 onconfiguring 并尝试查看环境并根据环境返回不同的连接字符串。但是,在部分类中,我们无法像从控制器那样访问环境。
如何检查从 EF Core 脚手架命令生成的 DAL 层中的当前环境。让脚手架命令本身读取不同的连接字符串没有问题,但它需要在 DAL 本身中发生,因此我们可以部署到 live 而不必首先使用新的连接字符串重新脚手架。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BCrypt.Net-Core" Version="1.6.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
<PackageReference Include="Xero.Api.SDK.Core" Version="1.1.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Hosting">
<HintPath>..\..\..\Program Files (x86)\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.hosting\2.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Hosting.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Http.Abstractions">
<HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.http.abstractions\2.0.1\lib\netstandard2.0\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Hosting">
<HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.hosting\2.2.0\lib\netstandard2.0\Microsoft.Extensions.Hosting.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
解决方案
您需要同时注入DbContextOptions
和IHostingEnvironment
。
尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using App.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting; // this is required
using System.ComponentModel.DataAnnotations.Schema;
// ...
public class AppDbContext : IdentityDbContext<IdentityUser>
{
public AppDbContext (DbContextOptions<AppDbContext> options,IHostingEnvironment env)
: base(options)
{
this._env = env;
}
[NotMapped]
private IHostingEnvironment _env;
protected override void OnModelCreating(ModelBuilder builder){
base.OnModelCreating(builder);
System.Console.WriteLine("*********************");
System.Console.WriteLine(this._env.EnvironmentName);
System.Console.WriteLine("*********************");
}
// your entities generated by scaffolding
public DbSet<App.Models.XModel> XModel { get; set; }
}
推荐阅读
- ruby-on-rails - Rails 按原样转发来自外部 API 的响应
- typo3 - TYPO3 9 中 sysext 表单的 JavaScript 错误
- java - 为什么这总是为布尔返回false?
- pytorch - Pytorch - 索引一系列多个索引?
- android - 是否可以再次使用具有相同 putextra 值的相同意图?
- python - 从 api 响应中解析嵌套 json 的最佳方法
- c - malloc() 到底是做什么的?
- javascript - 如何在没有 console.log() 的情况下记录 JavaScript 变量的值?
- javascript - 如何在初始页面加载之前加载 CSS 数据主题以防止主题之间闪烁?
- python - django项目中实时聊天的问题