Microsoft.Data.SqlClient.SqlException (0x80131904)

Microsoft.Data.SqlClient.SqlException (0x80131904)

我根据我创建的新实体生成了一个迁移,涉及网站的全部返工,而我必须保留旧的 tables 而不更改它们。

所以,首先,迁移生成成功。 请注意,我忽略了 OnModelCreating 中的所有 tables,例如:

    modelBuilder.Entity<AdresseModel>().ToTable(nameof(AdresseModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<ClasseTauxTVAModel>().ToTable(nameof(ClasseTauxTVAModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<ClientModel>().ToTable(nameof(ClientModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<ContactModel>().ToTable(nameof(ContactModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<DevisProduitModel>().ToTable(nameof(DevisProduitModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<DomaineModel>().ToTable(nameof(DomaineModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<DossierModel>().ToTable(nameof(DossierModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<EntiteModel>().ToTable(nameof(EntiteModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<FactureModel>().ToTable(nameof(FactureModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<LangueModel>().ToTable(nameof(LangueModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<MonnaieModel>().ToTable(nameof(MonnaieModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<PaysModel>().ToTable(nameof(PaysModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<ProduitDossierFactureModel>()
        .ToTable(nameof(ProduitDossierFactureModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<ProduitDossierModel>().ToTable(nameof(ProduitDossierModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<ProfilModel>().ToTable(nameof(ProfilModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<RoleModel>().ToTable(nameof(RoleModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<SousDomaineModel>().ToTable(nameof(SousDomaineModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<TacheModel>().ToTable(nameof(TacheModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<TacheProduitModel>().ToTable(nameof(TacheProduitModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<TagModel>().ToTable(nameof(TagModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<TauxTVAModel>().ToTable(nameof(TauxTVAModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<TimeTrackingModel>().ToTable(nameof(TimeTrackingModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<TraductionModel>().ToTable(nameof(TraductionModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<TypeUniteProduitModel>()
        .ToTable(nameof(TypeUniteProduitModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<UtilisateurAssocieModel>()
        .ToTable(nameof(UtilisateurAssocieModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<UtilisateurModel>().ToTable(nameof(UtilisateurModel), t => t.ExcludeFromMigrations());
    modelBuilder.Entity<ProduitModel>().ToTable(nameof(ProduitModel), t => t.ExcludeFromMigrations());

然后,我得到了这个新的 table,它在旧的 table(即 DossierModel)上创建了一个外键:

[Table("ProjetDossier")]
public class ProjetDossierModel
{
    public int Id { get; set; }
    public int Id_Dossier { get; set; }
    [ForeignKey("Id_Dossier")] public DossierModel Dossier { get; set; }
    [InverseProperty("ProjetDossier")] public ProjetModel Projet { get; set; }
}

因此,当我执行 dotnet ef database update 时,会抛出一个错误,提示我 table ProjetDossier 引用了无效的 table DossierModel。

我不知道如何绕过它,因为我必须在添加外键时将它从迁移中排除。

我找到了解决方案。 在我的迁移 xxx_InitialCreate.cs 中,排除的 table 的 auto-generated table 名称类似于 DossierModel 而不是 Dossier。 我只需更改:

            migrationBuilder.CreateTable(
                name: "ProjetDossier",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Id_Dossier = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ProjetDossier", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ProjetDossier_DossierModel_Id_Dossier",
                        column: x => x.Id_Dossier,
                        principalTable: "DossierModel",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.NoAction);
                });

收件人:

            migrationBuilder.CreateTable(
                name: "ProjetDossier",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Id_Dossier = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ProjetDossier", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ProjetDossier_DossierModel_Id_Dossier",
                        column: x => x.Id_Dossier,
                        principalTable: "Dossier",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.NoAction);
                });