Unable to add migration -

So i am having a working ASP.NET application running .NET 8.0. At first everything runs normally but when i try to add some migrations with the Add-Migration command, an error pop up as follows:
PM> Add-Migration InitialCreate
Build started...
Build succeeded.
Unable to create a 'DbContext' of type ''. The exception 'Cannot use table 'SyllabusTrainingProgram' for entity type 'SyllabusTrainingProgram (Dictionary<string, object>)' since it is being used for entity type 'SyllabusTrainingProgram' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'SyllabusTrainingProgram (Dictionary<string, object>)' on the primary key properties and pointing to the primary key on another entity type mapped to 'SyllabusTrainingProgram'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
PM>
PM> Add-Migration InitialCreate
Build started...
Build succeeded.
Unable to create a 'DbContext' of type ''. The exception 'Cannot use table 'SyllabusTrainingProgram' for entity type 'SyllabusTrainingProgram (Dictionary<string, object>)' since it is being used for entity type 'SyllabusTrainingProgram' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'SyllabusTrainingProgram (Dictionary<string, object>)' on the primary key properties and pointing to the primary key on another entity type mapped to 'SyllabusTrainingProgram'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
PM>
I have already tried to add the following lines of code inside the OnModelCreating method of the DbContext to create a relationship to the child models...
// Other pre-scaffoled code

modelBuilder.Entity<SyllabusTrainingProgram>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__SysTrainC__3213E83F0F533B87");

entity.HasOne(d => d.syllabus).WithMany(p => p.SyllabusTrainingPrograms)
.HasPrincipalKey(p => p.SyllabusId)
.HasForeignKey(d => d.SyllabusId)
.HasConstraintName("FK_SyllabusTrainingProgram_Syllabus");

entity.HasOne(d => d.program).WithMany(p => p.SyllabusTrainingPrograms)
.HasPrincipalKey(p => p.TrainingProgramCode)
.HasForeignKey(d => d.TrainingProgramCode)
.HasConstraintName("FK_SyllabusTrainingProgram_Program");
});

OnModelCreatingPartial(modelBuilder);
// Other pre-scaffoled code

modelBuilder.Entity<SyllabusTrainingProgram>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__SysTrainC__3213E83F0F533B87");

entity.HasOne(d => d.syllabus).WithMany(p => p.SyllabusTrainingPrograms)
.HasPrincipalKey(p => p.SyllabusId)
.HasForeignKey(d => d.SyllabusId)
.HasConstraintName("FK_SyllabusTrainingProgram_Syllabus");

entity.HasOne(d => d.program).WithMany(p => p.SyllabusTrainingPrograms)
.HasPrincipalKey(p => p.TrainingProgramCode)
.HasForeignKey(d => d.TrainingProgramCode)
.HasConstraintName("FK_SyllabusTrainingProgram_Program");
});

OnModelCreatingPartial(modelBuilder);
but no use. Since the code are too long, i have already put it here https://bpa.st/GB7A which contains the code of the entities that i doubt caused this problem. I'd appreciate if someone help me out.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server