如何从项目根目录获取Appsettings到ASP.NET Core 2.0和EF Core 2.0中的IDesignTimeDbContextFactory实现 [英] How to get appsettings from project root to IDesignTimeDbContextFactory implementation in ASP.NET Core 2.0 and EF Core 2.0

查看:39
本文介绍了如何从项目根目录获取Appsettings到ASP.NET Core 2.0和EF Core 2.0中的IDesignTimeDbContextFactory实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ASP.NET Core 2.0中构建应用程序,但是EntityFramework Migrations出现问题.

I am building an application in ASP.NET Core 2.0 and I am having problems with EntityFramework Migrations.

我在一个单独的项目( SolutionName \ ProjectNamePrefix .Data)中有我的DbContext,因此我为IDesignTimeDbContextFactory接口创建了一个实现.

I have my DbContext in a separate project (SolutionName\ProjectNamePrefix.Data) and therefore I created an implementation for the IDesignTimeDbContextFactory interface.

我想在不同的环境中使用不同的连接字符串,为此我需要appsettings.json.

I wanted to use different connection strings for different environments and I need appsettings.json for that.

因此,在快速搜索之后,我发现可以在CreateDbContext函数内部创建一个新的IConfigurationRoot对象,如下所示: https://codingblast.com/entityframework-core-idesigntimedbcontextfactory/

So after a quick search I found that I can create a new IConfigurationRoot object inside the CreateDbContext function as shown here: https://codingblast.com/entityframework-core-idesigntimedbcontextfactory/

我添加了该内容,然后进行测试,尝试从Data项目的根文件夹中运行dotnet ef migrations list -c MyContext.

I added that and then for testing, tried to run dotnet ef migrations list -c MyContext from the Data project root folder.

然后我遇到以下错误:

The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'C:\dev\*SolutionName*\*ProjectNamePrefix*.Data\bin\Debug\netcoreapp2.0\appsettings.json'.

因此,基本上,我尝试了3种方法来获取正确的根路径:

So, basically, I tried 3 options for getting the correct root path:

  • Directory.GetCurrentDirectory();
  • env.ContentRootPath; (IHostingEnvironment object, I found a way to get it here: https://github.com/aspnet/Home/issues/2194)
  • AppDomain.CurrentDomain.BaseDirectory;

,它们都返回相同的..\bin\debug\netcoreapp2.0\路径.当我从VS运行Data项目时,两个第一个选项为我提供了正确的项目根文件夹.

and all of them returned the same ..\bin\debug\netcoreapp2.0\ path. When I run the Data project from VS, then the two first options give me the correct project root folder.

是否可以获取正确的项目内容根文件夹?

因为当我向EF命令添加--verbose时,它注销了一行:

Because when I added --verbose to the EF command, it logged out a row:

Using content root 'C:\dev\FitsMeIdentity\FitsMeIdentity.Data\'.

因此,我了解到EF以某种方式知道项目的根目录,但是上述所有选项都返回了已构建应用程序的路径.

So I understand that EF somehow knows the project root but all the options mentioned above return the path for the already built application.

我发现可行的唯一选择是将Copy output to root folder更改为Copy always,但从此处找到:

The only option I found that works is that I change Copy output to root folder to Copy always but found from here: https://www.benday.com/2017/02/17/ef-core-migrations-without-hard-coding-a-connection-string-using-idbcontextfactory/ that it's not a good idea.

起初,我什至考虑过为IDesignTimeDbContextFactory实现创建一个构造函数,该构造函数将IOptions作为参数,但没有用,存在与此处说明的问题相同的问题: 与启动prj& amp;在不同的类库中,将Env Conn字符串注入具有EF Core DbContext的.NET Core 2.0中.实现IDesignTimeDbContextFactory

At first I even thought about creating a Constructor for the IDesignTimeDbContextFactory implementation which gets IOptions as a parameter but that didn't work, had the same problem as explained here: Injecting Env Conn String into .NET Core 2.0 w/EF Core DbContext in different class lib than Startup prj & implementing IDesignTimeDbContextFactory

推荐答案

否.您无法做到这一点,更重要的是:您不应该这样做. IDesignTimeDbContextFactory的全部要点是,这是一种在没有ASP.NET Core框架可以使用的上下文中(即从类库中)获取DbContext实例的方法.如果您正在从ASP.NET Core项目运行迁移,则不需要它,如果没有,则没有任何配置可用.

No. You can't do this, and more to the point: you're not supposed to do this. The whole entire point of IDesignTimeDbContextFactory is that it's a way to get a DbContext instance from in a context where there is no ASP.NET Core framework to work with, i.e. from a class library. If you're running migrations from an ASP.NET Core project, you don't need it, and if you're not, none of the configuration stuff is available.

此外,它仅用于开发,因此是名称的"DesignTime"部分.结果,不需要诸如在不同环境的连接字符串之间切换之类的东西.只需将连接字符串硬编码为文档详细信息.

Additionally, it's only to be used for development, hence the "DesignTime" part of the name. As a result, there's no need for stuff like switching between connection strings for different environments. Just hard-code the connection string as the docs detail.

这篇关于如何从项目根目录获取Appsettings到ASP.NET Core 2.0和EF Core 2.0中的IDesignTimeDbContextFactory实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆