RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1539438
Accepted
air
air
Asked:2023-09-07 10:06:19 +0000 UTC2023-09-07 10:06:19 +0000 UTC 2023-09-07 10:06:19 +0000 UTC

42P07:关系“用户”已经存在

  • 772

我尝试通过迁移添加新表,但收到用户已存在的错误。

我的操作顺序如下: dotnet ef 迁移删除 -> 通过 postgres 终端手动清除数据库 -> dotnet ef 迁移添加 InitialMigrations -> dotnet ef 数据库更新(错误!)

虽然数据库本身没有表,但之前也有错误,已经绝望了,我拆除数据库并创建一个新数据库,没有任何表,但是当我尝试更新数据库时,出现以下错误:

失败:Microsoft.EntityFrameworkCore.Database.Command[20102] 执行 DbCommand 失败(17ms)[Parameters=[],CommandType='Text',CommandTimeout='30']

CREATE TABLE users (
id integer GENERATED BY DEFAULT AS IDENTITY,
role text NULL,
username text NULL,
password_hash text NULL,
email text NULL,
encryption_key text NULL,
api_key text NULL,
CONSTRAINT "PK_users" PRIMARY KEY (id)
);

执行 DbCommand 失败(17 毫秒)[参数=[],CommandType='Text',CommandTimeout='30']

CREATE TABLE users (
id integer GENERATED BY DEFAULT AS IDENTITY,
role text NULL,
username text NULL,
password_hash text NULL,
email text NULL,
encryption_key text NULL,
api_key text NULL,
CONSTRAINT "PK_users" PRIMARY KEY (id)
);
Npgsql.PostgresException (0x80004005): 42P07: relation "users" already exists

   at Npgsql.Internal.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|234_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
   at Npgsql.NpgsqlDataReader.NextResult()
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
  Exception data:
    Severity: ERROR
    SqlState: 42P07
    MessageText: relation "users" already exists
    File: heap.c
    Line: 1148
    Routine: heap_create_with_catalog

42P07: relation "users" already exists

虽然数据库是干净的并且不包含任何表

在此输入图像描述

这是文件模型:

public class UserFileModel
{
    public string? fileId { get; set; }
    public string? file_path { get; set; }
    public string? file_name { get; set; }
    public string? status { get; set; }
    public int? result_scan { get; set; }
    public string error_message { get; set; }
    public string enc_time { get; set; }
    public string dec_time { get; set; }
    public string upload_time { get; set; }
    public string download_time { get; set; }

    public int? id { get; set; } // Внешний ключ

    [ForeignKey("id")]
    public UserModel User { get; set; } // Навигационное свойство
}

这是用户模型

public class UserModel
{
    public int? id { get; set; }
    public string? role { get; set; }
    public string? username { get; set; }
    public string? password_hash { get; set; }
    public string? email { get; set; }
    public string? encryption_key { get; set; }
    public string? api_key { get; set; }
}

这是上下文类

public class FileCryptDbContext : DbContext
{
    public DbSet<UserModel> Users { get; set; }
    public DbSet<UserFileModel> Files { get; set; }

    public FileCryptDbContext(DbContextOptions<FileCryptDbContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserFileModel>()
            .HasOne(f => f.User)
            .WithMany()
            .HasForeignKey(f => f.id);
    }
}

这是迁移过程中创建的 ModelSnapShot。

[DbContext(typeof(FileCryptDbContext))]
partial class FileCryptDbContextModelSnapshot : ModelSnapshot
{
    protected override void BuildModel(ModelBuilder modelBuilder)
    {
#pragma warning disable 612, 618
        modelBuilder
            .HasAnnotation("ProductVersion", "7.0.10")
            .HasAnnotation("Relational:MaxIdentifierLength", 63);

        NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

        modelBuilder.Entity("FileCryptWeb.Models.UserFileModel", b =>
            {
                b.Property<int?>("id")
                    .HasColumnType("integer");

                b.Property<string>("dec_time")
                    .IsRequired()
                    .HasColumnType("text");

                b.Property<string>("download_time")
                    .IsRequired()
                    .HasColumnType("text");

                b.Property<string>("enc_time")
                    .IsRequired()
                    .HasColumnType("text");

                b.Property<string>("error_message")
                    .IsRequired()
                    .HasColumnType("text");

                b.Property<string>("fileId")
                    .HasColumnType("text");

                b.Property<string>("file_name")
                    .HasColumnType("text");

                b.Property<string>("file_path")
                    .HasColumnType("text");

                b.Property<int?>("result_scan")
                    .HasColumnType("integer");

                b.Property<string>("status")
                    .HasColumnType("text");

                b.Property<string>("upload_time")
                    .IsRequired()
                    .HasColumnType("text");

                b.HasKey("id");

                b.ToTable("files");
            });

        modelBuilder.Entity("FileCryptWeb.Models.UserModel", b =>
            {
                b.Property<int?>("id")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("integer");

                NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int?>("id"));

                b.Property<string>("api_key")
                    .HasColumnType("text");

                b.Property<string>("email")
                    .HasColumnType("text");

                b.Property<string>("encryption_key")
                    .HasColumnType("text");

                b.Property<string>("password_hash")
                    .HasColumnType("text");

                b.Property<string>("role")
                    .HasColumnType("text");

                b.Property<string>("username")
                    .HasColumnType("text");

                b.HasKey("id");

                b.ToTable("users");
            });

        modelBuilder.Entity("FileCryptWeb.Models.UserFileModel", b =>
            {
                b.HasOne("FileCryptWeb.Models.UserModel", "User")
                    .WithMany()
                    .HasForeignKey("id")
                    .OnDelete(DeleteBehavior.Cascade)
                    .IsRequired();

                b.Navigation("User");
            });
#pragma warning restore 612, 618
    }
}
c#
  • 1 1 个回答
  • 35 Views

1 个回答

  • Voted
  1. Best Answer
    air
    2023-09-07T10:40:12Z2023-09-07T10:40:12Z

    一切都变得非常明显和简单。删除迁移后,我没有更新数据库。于是输入这个命令就解决了所有问题。

    dotnet ef database update
    
    • 0

相关问题

  • 使用嵌套类导出 xml 文件

  • 分层数据模板 [WPF]

  • 如何在 WPF 中为 ListView 手动创建列?

  • 在 2D 空间中,Collider 2D 挂在玩家身上,它对敌人的重量相同,我需要它这样当它们碰撞时,它们不会飞向不同的方向。统一

  • 如何在 c# 中使用 python 神经网络来创建语音合成?

  • 如何知道类中的方法是否属于接口?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5