© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago
ScriptKidding

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
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
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ Add-Migration
C#CC# / help
3y ago
Issues with EntityFramework. Unable to add-migrations, enable-migrations.
C#CC# / help
2y ago
EF-Core unable to create migration
C#CC# / help
2y ago
✅ Unable to create migration - 'Unable to resolve service for type 'DbContextOptions'
C#CC# / help
2y ago