已部署的 .NET Core 应用程序(其中打包了数据库迁移)不会在服务器上创建新数据库。我该如何强制这样做?



所以我有很多这样的迁移文件,当我在本地启动应用程序时,它们可以工作并应用它们的更改,没有任何问题。

我还定义了我的连接字符串和数据库名称,并有四重检查它是正确的。但是,当我在服务器上部署web应用程序时,没有创建数据库,也没有应用迁移。我认为,当应用程序在服务器上运行时,数据库将由应用程序创建。

我错过了什么?

示例迁移文件:

using Microsoft.EntityFrameworkCore.Migrations;
namespace Persistence.Migrations
{
public partial class modificationstoprivsandroles : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Name",
table: "UserRoles");
migrationBuilder.DropColumn(
name: "Name",
table: "Tags");
migrationBuilder.DropColumn(
name: "Name",
table: "Privileges");
migrationBuilder.AddColumn<int>(
name: "Type",
table: "UserRoles",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "Type",
table: "Tags",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "Type",
table: "Privileges",
nullable: false,
defaultValue: 0);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Type",
table: "UserRoles");
migrationBuilder.DropColumn(
name: "Type",
table: "Tags");
migrationBuilder.DropColumn(
name: "Type",
table: "Privileges");
migrationBuilder.AddColumn<string>(
name: "Name",
table: "UserRoles",
type: "longtext CHARACTER SET utf8mb4",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "Name",
table: "Tags",
type: "longtext CHARACTER SET utf8mb4",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "Name",
table: "Privileges",
type: "longtext CHARACTER SET utf8mb4",
nullable: true);
}
}
}

数据上下文
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//modelBuilder.Model.SetMaxIdentifierLength(30);
////Attempt 1:
////https://stackoverflow.com/questions/58821587/pomelo-entityframeworkcore-mysql-error-with-ef-core-3-0
//modelBuilder.Entity<AppUser>(entity => entity.Property(m => m.Id).HasMaxLength(255));
////Other attempt.
////modelBuilder.Entity<HistoryRow>().Property(h => h.MigrationId).HasMaxLength(100).IsRequired();
////modelBuilder.Entity<HistoryRow>().Property(h => h.ContextKey).HasMaxLength(200).IsRequired();
////https://stackoverflow.com/questions/20832546/entity-framework-with-mysql-and-migrations-failing-because-max-key-length-is-76
modelBuilder.Entity<Value>().HasData(
new Value { Id = 1, Name = "Value 1331" },
new Value { Id = 2, Name = "Value 1332" },
new Value { Id = 3, Name = "Value 1333" }
);
...

甚至有一个种子数据文件。

namespace Persistence
{
public class Seed
{
public static async Task SeedData(DataContext context,
UserManager<AppUser> userManager)
{
if (!context.Privileges.Any())
{
var privileges = new List<Privilege>
{
new Privilege
{
Id = Guid.Parse("11e36cdd-b1a0-489b-ad79-9e926eece529"),
Type = Privilege.PrivilegeTypes.ADMIN,
Description = "Administrator role"
}
};
await context.Privileges.AddRangeAsync(privileges);
await context.SaveChangesAsync();
}
...

真的不知道为什么数据库不直接在这里启动。

你可以尝试在Startup.cs中执行你的迁移吗->配置方法

using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<DataContext>();
context.Database.Migrate();
}

这将从迁移中创建数据库。如果您的迁移中没有创建代码,您可以尝试这样做:

  1. 从SQL Server对象资源管理器中删除数据库
  2. 从migrations文件夹中删除所有现有的迁移。
  3. 在Package-Management-Console中输入"Add-Migration InitialCreate"[可选,取决于你的数据库初始化器]
  4. 在Package-Management-Console中输入&;update-database&;

改编自:如何使用代码优先迁移创建数据库?和自动创建实体框架核心数据库

相关内容

最新更新