实体框架错误插入或更新表“TblScheme”违反外键约束“FK_TblScheme_TblCompanies_CompanyId”

问题描述 投票:0回答:0

每次我尝试更新我的数据库迁移时都会收到此错误:

Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (23ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      ALTER TABLE "TblScheme" ADD CONSTRAINT "FK_TblScheme_TblCompanies_CompanyId" FOREIGN KEY ("CompanyId") REFERENCES "TblCompanies" ("paramID") ON DELETE CASCADE;
Failed executing DbCommand (23ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE "TblScheme" ADD CONSTRAINT "FK_TblScheme_TblCompanies_CompanyId" FOREIGN KEY ("CompanyId") REFERENCES "TblCompanies" ("paramID") ON DELETE CASCADE;
Npgsql.PostgresException (0x80004005): 23503: insert or update on table "TblScheme" violates foreign key constraint "FK_TblScheme_TblCompanies_CompanyId"

DETAIL: Key (CompanyId)=(0) is not present in table "TblCompanies".
   at Npgsql.Internal.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|215_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()
   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: 23503
    MessageText: insert or update on table "TblScheme" violates foreign key constraint "FK_TblScheme_TblCompanies_CompanyId"
    Detail: Key (CompanyId)=(0) is not present in table "TblCompanies".
    SchemaName: public
    TableName: TblScheme
    ConstraintName: FK_TblScheme_TblCompanies_CompanyId
    File: ri_triggers.c
    Line: 2539
    Routine: ri_ReportViolation
23503: insert or update on table "TblScheme" violates foreign key constraint "FK_TblScheme_TblCompanies_CompanyId"

DETAIL: Key (CompanyId)=(0) is not present in table "TblCompanies".

当我尝试在现有表中建立新关系时会发生这种情况,作为一种解决方法,我不得不删除并重新创建表,但效率不高。这样做时我缺少任何步骤吗?

c# postgresql entity-framework
© www.soinside.com 2019 - 2024. All rights reserved.