【问题标题】:SQLite does not support this migration operation ('AlterColumnOperation')SQLite 不支持此迁移操作 ('AlterColumnOperation')
【发布时间】:2020-09-25 14:08:39
【问题描述】:

这是我的 DbContext:

using DatingApp.API.Models;
using Microsoft.EntityFrameworkCore;

namespace DatingApp.API.Data
{
    public class DataContext : DbContext
    {
        public DataContext(DbContextOptions<DataContext> options) : base(options){ }

        public DbSet<Value> Values { get; set; }
        public DbSet<User> User { get; set; }
        public DbSet<Photo> Photos { get; set; }
    }
}

这是我生成的文件迁移:

using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace DatingApp.API.Migrations
{
    public partial class ExtendedUserClass : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<string>(
                name: "Username",
                table: "User",
                nullable: true,
                oldClrType: typeof(int));

            migrationBuilder.AddColumn<string>(
                name: "City",
                table: "User",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Country",
                table: "User",
                nullable: true);

            migrationBuilder.AddColumn<DateTime>(
                name: "Created",
                table: "User",
                nullable: false,
                defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

            migrationBuilder.AddColumn<DateTime>(
                name: "DateOfBirth",
                table: "User",
                nullable: false,
                defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

            migrationBuilder.AddColumn<string>(
                name: "Gender",
                table: "User",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Interests",
                table: "User",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Introduction",
                table: "User",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "KnownAs",
                table: "User",
                nullable: true);

            migrationBuilder.AddColumn<DateTime>(
                name: "LastActive",
                table: "User",
                nullable: false,
                defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

            migrationBuilder.AddColumn<string>(
                name: "LookingFor",
                table: "User",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "Photos",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Url = table.Column<string>(nullable: true),
                    Description = table.Column<string>(nullable: true),
                    DateAdded = table.Column<DateTime>(nullable: false),
                    IsMain = table.Column<bool>(nullable: false),
                    UserId = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Photos", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Photos_User_UserId",
                        column: x => x.UserId,
                        principalTable: "User",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Photos_UserId",
                table: "Photos",
                column: "UserId");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Photos");

            migrationBuilder.DropColumn(
                name: "City",
                table: "User");

            migrationBuilder.DropColumn(
                name: "Country",
                table: "User");

            migrationBuilder.DropColumn(
                name: "Created",
                table: "User");

            migrationBuilder.DropColumn(
                name: "DateOfBirth",
                table: "User");

            migrationBuilder.DropColumn(
                name: "Gender",
                table: "User");

            migrationBuilder.DropColumn(
                name: "Interests",
                table: "User");

            migrationBuilder.DropColumn(
                name: "Introduction",
                table: "User");

            migrationBuilder.DropColumn(
                name: "KnownAs",
                table: "User");

            migrationBuilder.DropColumn(
                name: "LastActive",
                table: "User");

            migrationBuilder.DropColumn(
                name: "LookingFor",
                table: "User");

            migrationBuilder.AlterColumn<int>(
                name: "Username",
                table: "User",
                nullable: false,
                oldClrType: typeof(string),
                oldNullable: true);
        }
    }
}

尝试删除数据库迁移 我更新,从头添加迁移,尝试执行dotnet ef数据库更新命令,得到消息:

SQLite does not support this migration operation ('AlterColumnOperation'). For more information, see http://go.microsoft.com/fwlink/?LinkId=723262.

我不知道如何在 SQL lite 中添加迁移,这就是问题出现的原因。

如何更改列?因为大概就是这样。

【问题讨论】:

  • 基本上:您更改列。 SQLite 不支持它。您可以改为编写自己的迁移来创建一个完全 new 表,然后 SELECT INTO 该表。 (这些迁移问题实际上是我寻找 SQLite 替代品的原因)
  • (忘记了“更改”之前的“不能”,但意识到编辑为时已晚)
  • Franz 感谢您的回答,但我正在手动添加迁移

标签: c# asp.net asp.net-core


【解决方案1】:

您可以删除数据库和所有现有迁移并再次运行add-migration InitialCreate,这将创建一个从头开始的新迁移,因此不会有任何列需要更改。

【讨论】:

  • YStroli 您的解决方案有帮助
  • 最佳解决方案...在更新数据库之前添加迁移
【解决方案2】:

您可以执行以下命令:

dotnet ef migrations remove 
dotnet ef migrations add initialCommit
dotnet ef database update

但最好的解决方案是当我在 Migration 类文件中注释掉 AlterColumn 行时,它编译成功,并且也创建了我的数据库

但教会了我一个教训:不要在 SQLite 中使用代码优先

【讨论】:

    猜你喜欢
    • 2020-07-16
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 2020-11-07
    • 2011-05-04
    • 2011-09-11
    • 2017-04-30
    • 1970-01-01
    相关资源
    最近更新 更多