【问题标题】:Error in command update-migration, Entity Framework 3.15, Net Core 3.1命令更新迁移、Entity Framework 3.15、Net Core 3.1 中的错误
【发布时间】:2021-08-24 07:12:53
【问题描述】:

我已经定义了我的模板并在 DbContext 中引用,但是当我运行 update-database 命令时,它会生成错误:找不到“InstitutionContact”对象,因为它不存在或没有权限。

验证它创建的迁移,可以清楚地看到产生了错误,因为它创建了一个不存在的表的更新迁移,而不是表创建迁移。如果我要更新的表不存在,如何避免创建更新迁移?如何创建我需要的表?

迁移

using Microsoft.EntityFrameworkCore.Migrations;

命名空间 DegreeProjectsSystem.DataAccess.Migrations { 公共部分类 AddTableInstitutionContacts :迁移 { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.DropForeignKey( 名称:“FK_InstitutionContact_InstitutionContactCharges_InstitutionContactChargeId”, 表:“机构联系方式”);

        migrationBuilder.DropForeignKey(
            name: "FK_InstitutionContact_Institutions_InstitutionId",
            table: "InstitutionContact");

        migrationBuilder.DropForeignKey(
            name: "FK_InstitutionContact_People_PersonId",
            table: "InstitutionContact");

        migrationBuilder.RenameTable(
            name: "InstitutionContact",
            newName: "InstitutionContacts");

        migrationBuilder.AlterColumn<int>(
            name: "PersonId",
            table: "InstitutionContacts",
            nullable: false,
            oldClrType: typeof(int),
            oldNullable: true);

        migrationBuilder.AlterColumn<int>(
            name: "InstitutionId",
            table: "InstitutionContacts",
            nullable: false,
            oldClrType: typeof(int),
            oldNullable: true);

        migrationBuilder.AlterColumn<int>(
            name: "InstitutionContactChargeId",
            table: "InstitutionContacts",
            nullable: false,
            oldClrType: typeof(int),
            oldNullable: true);

        migrationBuilder.AddColumn<int>(
            name: "Id",
            table: "InstitutionContacts",
            nullable: false,
            defaultValue: 0)
            .Annotation("SqlServer:Identity", "1, 1");

        migrationBuilder.AddColumn<bool>(
            name: "Active",
            table: "InstitutionContacts",
            nullable: false,
            defaultValue: false);

        migrationBuilder.AddPrimaryKey(
            name: "PK_InstitutionContacts",
            table: "InstitutionContacts",
            column: "Id");

        migrationBuilder.CreateIndex(
            name: "IX_InstitutionContacts_InstitutionContactChargeId",
            table: "InstitutionContacts",
            column: "InstitutionContactChargeId");

        migrationBuilder.CreateIndex(
            name: "IX_InstitutionContacts_PersonId",
            table: "InstitutionContacts",
            column: "PersonId");

        migrationBuilder.CreateIndex(
            name: "IX_InstitutionContacts_InstitutionId_PersonId_InstitutionContactChargeId",
            table: "InstitutionContacts",
            columns: new[] { "InstitutionId", "PersonId", "InstitutionContactChargeId" },
            unique: true);

        migrationBuilder.AddForeignKey(
            name: "FK_InstitutionContacts_InstitutionContactCharges_InstitutionContactChargeId",
            table: "InstitutionContacts",
            column: "InstitutionContactChargeId",
            principalTable: "InstitutionContactCharges",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);

        migrationBuilder.AddForeignKey(
            name: "FK_InstitutionContacts_Institutions_InstitutionId",
            table: "InstitutionContacts",
            column: "InstitutionId",
            principalTable: "Institutions",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);

        migrationBuilder.AddForeignKey(
            name: "FK_InstitutionContacts_People_PersonId",
            table: "InstitutionContacts",
            column: "PersonId",
            principalTable: "People",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropForeignKey(
            name: "FK_InstitutionContacts_InstitutionContactCharges_InstitutionContactChargeId",
            table: "InstitutionContacts");

        migrationBuilder.DropForeignKey(
            name: "FK_InstitutionContacts_Institutions_InstitutionId",
            table: "InstitutionContacts");

        migrationBuilder.DropForeignKey(
            name: "FK_InstitutionContacts_People_PersonId",
            table: "InstitutionContacts");

        migrationBuilder.DropPrimaryKey(
            name: "PK_InstitutionContacts",
            table: "InstitutionContacts");

        migrationBuilder.DropIndex(
            name: "IX_InstitutionContacts_InstitutionContactChargeId",
            table: "InstitutionContacts");

        migrationBuilder.DropIndex(
            name: "IX_InstitutionContacts_PersonId",
            table: "InstitutionContacts");

        migrationBuilder.DropIndex(
            name: "IX_InstitutionContacts_InstitutionId_PersonId_InstitutionContactChargeId",
            table: "InstitutionContacts");

        migrationBuilder.DropColumn(
            name: "Id",
            table: "InstitutionContacts");

        migrationBuilder.DropColumn(
            name: "Active",
            table: "InstitutionContacts");

        migrationBuilder.RenameTable(
            name: "InstitutionContacts",
            newName: "InstitutionContact");

        migrationBuilder.AlterColumn<int>(
            name: "PersonId",
            table: "InstitutionContact",
            nullable: true,
            oldClrType: typeof(int));

        migrationBuilder.AlterColumn<int>(
            name: "InstitutionId",
            table: "InstitutionContact",
            nullable: true,
            oldClrType: typeof(int));

        migrationBuilder.AlterColumn<int>(
            name: "InstitutionContactChargeId",
            table: "InstitutionContact",
            nullable: true,
            oldClrType: typeof(int));

        migrationBuilder.AddForeignKey(
            name: "FK_InstitutionContact_InstitutionContactCharges_InstitutionContactChargeId",
            table: "InstitutionContact",
            column: "InstitutionContactChargeId",
            principalTable: "InstitutionContactCharges",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);

        migrationBuilder.AddForeignKey(
            name: "FK_InstitutionContact_Institutions_InstitutionId",
            table: "InstitutionContact",
            column: "InstitutionId",
            principalTable: "Institutions",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);

        migrationBuilder.AddForeignKey(
            name: "FK_InstitutionContact_People_PersonId",
            table: "InstitutionContact",
            column: "PersonId",
            principalTable: "People",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
    }
}

}

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace DegreeProjectsSystem.Models
{
    public class InstitutionContact
    {
        [Key]
        public int Id { get; set; }
        
        [Required(AllowEmptyStrings = false, ErrorMessage = "Debe seleccionar una Institución")]
        public int InstitutionId { get; set; }
        
        [Display(Name = "Institución")]
        //Foreign key
        [ForeignKey("InstitutionId")]
        public Institution Institution { get; set; }
        
        [Required(AllowEmptyStrings = false, ErrorMessage = "Debe seleccionar un Contacto")]
        public int PersonId { get; set; }
        
        [Display(Name = "Contacto")]
        //Foreign key
        [ForeignKey("PersonId")]
        public Person Person { get; set; }
        
        [Required(AllowEmptyStrings = false, ErrorMessage = "Debe seleccionar el cargo del Contacto")]
        public int InstitutionContactChargeId { get; set; }
        
        [Display(Name = "Cargo")]
        //Foreign key
        [ForeignKey("InstitutionContactChargeId")]
        public InstitutionContactCharge InstitutionContactCharge { get; set; }
        
        [Display(Name = "Estado")]
        public bool Active { get; set; }
        }
        }
    }
}

【问题讨论】:

  • 尝试删除迁移文件中 Up 方法和 down 方法中的代码。然后运行命令更新数据库
  • 解决方案无效,它不会产生错误但也不会创建表。
  • 您是否曾经删除过任何迁移? Bevause 好像你做到了。您的模型快照可能已经包含该表的定义。

标签: c# entity-framework entity-framework-migrations


【解决方案1】:

解决办法是:

  • 我从项目中删除了所有迁移。
  • 我删除了
    数据库。
  • 我生成了第一个迁移。
  • 在运行更新迁移之前,我修改了一些主键 因创建级联而产生错误的关系 默认删除,改变 onDelete: ReferentialAction.Cascade 到 onDelete:ReferentialAction.NoAction。
  • 创建新的迁移以重建数据库。

【讨论】:

    猜你喜欢
    • 2020-08-22
    • 2017-07-03
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 2017-10-27
    • 1970-01-01
    相关资源
    最近更新 更多