首页 > 解决方案 > 如何从不使用任何形式的 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>

标签: asp.net-core

解决方案


您需要同时注入DbContextOptionsIHostingEnvironment

尝试以下操作:

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; }

    }

推荐阅读