如何将ASPNETCORE_ENVIRONMENT设置为生产? [英] How do I set ASPNETCORE_ENVIRONMENT to production?
问题描述
你好,我正在尝试在Windows计算机和 SQL Server EF核心迁移 -core-3-simple-api-for-authentication-registration-and-user-management#user-model-cs rel = nofollow noreferrer>教学课程我要说的是运行命令
Hello I am trying to do a SQL Server EF Core Migration on a Windows computer and the tutorial I am following says to run the command
set ASPNETCORE_ENVIRONMENT =生产
当我收到以下消息时:
dotnet ef迁移添加InitialCreate --context DataContext --output-dir迁移/ SqlServerMigrations
得到消息:
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services.
Continuing without the application service provider.
Error: Object reference not set to an instance of an object.
Unable to create an object of type 'DataContext'.
For the different patterns supported at design time,
see https://go.microsoft.com/fwlink/?linkid=851728
本教程说,此错误将来自未将环境设置为生产环境。我已经尝试运行上面列出的set命令,但是它似乎没有用。我的 launchSettings.json 中的环境变量没有变化。当我在 launchSettings.json 中将环境变量手动更改为生产时,migrations命令仍然不起作用。
The tutorial says that this error will come from not setting the environment to production. I have tried to run the set command listed above, but it doesn't appear to work. The environment variables in my launchSettings.json don't change. When I manually change the environment variable to production in launchSettings.json, the migrations command still doesn't work.
我有两个问题,是 set ASPNETCORE_ENVIRONMENT =生产
要运行正确的命令?如果我需要手动更改它,除了launchSettings.json之外,还需要更改其他地方吗?
I have two questions, is set ASPNETCORE_ENVIRONMENT=Production
the correct command to run? If I need to change it manually, where else other than launchSettings.json do I need to change?
作为参考,这是DataContext类的样子:
For reference, this is what the DataContext class looks like:
public class DataContext : DbContext
{
protected readonly IConfiguration Configuration;
public DataContext(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
// connect to sql server database
string conString = Configuration.GetConnectionString("WebApiDatabase");
options.UseSqlServer(conString);
}
public DbSet<User> Users { get; set; }
}
并在我的appsettings.json文件中(没有真正的细节,但格式相同):
and in my appsettings.json file (no real details, but same format):
"ConnectionStrings": {
"WebApiDatabase": "Data Source=dbServer;Initial Catalog=fakeDb;User ID=admin;Password=password"
}
我正在使用 3.1.402 作为 .net核心SDK 。我为 entity框架核心库安装了版本 3.1.8 。谢谢!
I am using 3.1.402 as the version for the .net core sdk. I have version 3.1.8 installed for the entity framework core library. Thank you!
推荐答案
当EF核心无法创建您的DbContext实例时,就会发生这种情况。尝试将以下类添加到您的项目中:
This happens when EF core is not able to create an instance of your DbContext. Try adding the following class to your project:
/// <summary>
/// Database context factory to be used during EF migrations.
/// </summary>
public class DataContextFactory : IDesignTimeDbContextFactory<DataContext>
{
/// <summary>
/// Creates a <see cref="DataContext"/>.
/// </summary>
/// <param name="args">Arguments.</param>
/// <returns>Database context.</returns>
public DataContext CreateDbContext(string[] args)
{
// NOTE:
// Right now, database migration can only be performed on Windows.
// A file called "temp.db" will be placed on the desktop and can be deleted after migration.
var builder = new DbContextOptionsBuilder<DataContext>();
builder.UseSqlite($"Data Source={Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "temp.db")}");
return new DataContext(builder.Options);
}
}
这应提供创建数据库所需的功能。在创建过程中,将在桌面上放置一个名为temp.db的文件。
This should provide the required functionality for creating the database. During creation a file called temp.db will be placed on your desktop.
根据需要更改数据库类型(UseSqlite)以及数据库文件的名称和路径。
Change the database type (UseSqlite) and the name and path of the database file according to your needs.
这篇关于如何将ASPNETCORE_ENVIRONMENT设置为生产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!