我无法在使用 SQLite 数据库的 ASP.NET Core Web 应用程序的发布版本上应用 ApplicationDbContext 的现有迁移。结果,我必须将我的 sqlite .db 文件从 Debug 构建(我能够应用迁移)复制到 Release 构建,以便我可以将它部署在我的 Ubuntu 服务器上。要么这样,要么我必须在生产环境中调用里面app.UseDatabaseErrorPage();
的方法,不推荐。public void Configure()
Startup.cs
要重现此问题,请在 Visual Studio 中使用个人用户帐户身份验证创建一个 ASP.NET Core Web 应用程序 (.NET Core)。我将我的项目称为Hevn。
然后,更新project.json
以使用 SQLite 而不是 SQL Server。
// Change from this
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.EntityFrameworkCore.SqlServer.Design": {
"version": "1.0.1",
"type": "build"
},
// to this
"Microsoft.EntityFrameworkCore.Sqlite": "1.0.1",
"Microsoft.EntityFrameworkCore.Sqlite.Design": {
"version": "1.0.1",
"type": "build"
},
然后,更新Startup.cs
以使用 SQLite 而不是 SQL Server:
// Change from this
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
// to this
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
然后,打开appsettings.json
并更改 DefaultConnection 以使用 SQLite 数据库。(我命名我的 Hevn.db):
// Change DefaultConnection to use SQLite database called Hevn.db
"ConnectionStrings": {
"DefaultConnection": "Data Source=./Hevn.db"
},
然后打开命令提示符并通过导航到项目并运行来应用迁移dotnet ef database update
cd "%USERPROFILE%/Documents/Visual Studio 2015/Projects/Hevn/src/Hevn"
dotnet ef database update
dotnet run
这将创建bin/Debug/netcoreapp1.0
文件夹,并且该文件夹内包含Hevn.db
sqlite 数据库文件。当我浏览sqlite文件时,可以看到已经创建了认证所需的表(即AspNetUsers
表)。
但是,我无法为发布版本执行此操作。我可以运行dotnet build -c Release
以在bin/Release/netcoreapp1.0
. 我也可以运行dotnet run -c Release
以运行我的应用程序的发布版本。运行Release版本会在下创建Hevn.db
sqlite文件bin/Release/netcoreapp1.0
,但是浏览sqlite文件显示没有创建表。
此时,我唯一的选择是将Hevn.db
文件从 Debug 复制到 Release 构建中。
我也试过dotnet ef database update -e Production
了,但仍然Hevn.db
在 Debug 构建下创建。
当我在应用迁移之前在调试模式下运行我的 ASP.NET Core Web 应用程序时,我在尝试注册/登录时收到以下屏幕。我可以简单地单击 Apply Migrations 或运行dotnet ef database update
以使用 SQLite 应用迁移和注册/登录工作。
但是,当我在发布模式下运行我的 ASP.NET Core Web 应用程序时,当我尝试注册/登录时会出现以下屏幕。我无法将迁移应用到发布版本。
是否可以在 ASP.NET Core Web 应用的发布版本上应用迁移?还是我必须求助于将数据库迁移的 sqlite 文件从 Debug 复制到 Release?