Home > database >  Microsoft.Data.SqlClient.SqlException (0x80131904)
Microsoft.Data.SqlClient.SqlException (0x80131904)

Time:04-20

I generated a migration based on new entities I have created, concerning a website total rework, while I have to keep old tables without changing them.

So, first, migrations generated successfuly. Note that I have ignored all tables from OnModelCreating, ex:

    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());

Then, I got this new table, that create a foreign key on an old table (that is 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; }
}

So when I do dotnet ef database update, an error is thrown that said me the table ProjetDossier is having a reference on the not valid table DossierModel.

I don't know how to bypass it since I have to exclude it from migrations while adding the foreign key.

CodePudding user response:

I found the solution. On my migration xxx_InitialCreate.cs, the auto-generated table name of excluded tables was like DossierModel instead of Dossier. I only had to change from:

            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);
                });

To:

            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);
                });
  • Related