如何不使用任何形式的DI

时间:2019-04-26 07:49:53

标签: asp.net-core

当DBA创建数据库时,我们正在使用Entity Framework从数据库中构建类,而我们不能先使用代码。

在脚手架上创建一个DbContext类并用硬编码到该类中的连接字符串填充onconfiguring方法时,我们有一个局部类重写了onconfiguring并尝试查看环境并根据该环境返回不同的连接字符串。但是,在部分类中,我们无法像从控制器那样访问环境。

我将如何检查由EF Core脚手架命令生成的DAL层中的当前环境。我们可以毫无疑问地使scaffold命令本身读取不同的连接字符串,但是它需要在DAL本身中发生,因此我们可以直接部署而不用先使用新的连接字符串重新折叠。

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

1 个答案:

答案 0 :(得分:0)

您需要同时注入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; }

    }