【发布时间】: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