我基于我創建的新物體生成了一個遷移,涉及網站整體返工,而我必須保留舊表而不更改它們。
因此,首先,遷移成功生成。請注意,我忽略了 OnModelCreating 中的所有表,例如:
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());
然后,我得到了這個新表,它在舊表(即 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,會拋出一個錯誤,告訴我表 ProjetDossier 對無效表 DossierModel 有參考。
我不知道如何繞過它,因為我必須在添加外鍵時將其從遷移中排除。
uj5u.com熱心網友回復:
我找到了解決方案。在我的遷移xxx_InitialCreate.cs中,排除表的自動生成的表名就像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);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459600.html
上一篇:物體框架:查詢與foreach
下一篇:如何為此方法引數傳遞文本框值?
