-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Razor Page SelectList not populating when using ViewModel #26228
-
Hi,
I feel like I'm doing exactly what the tutorial is saying in https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/update-related-data?view=aspnetcore-6.0 but my query to populate my select list is not returning any values. I am wondering if it's because I don't have foreign key relations on the database I inherited.
I see this in the Contuso sample but not sure if this is because the database is being created within the sample tutorial. Do I need this in my project too? Is this even related to my possible issue?
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity().ToTable(nameof(Course))
.HasMany(c => c.Instructors)
.WithMany(i => i.Courses);
modelBuilder.Entity().ToTable(nameof(Student));
modelBuilder.Entity().ToTable(nameof(Instructor));
}
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 2 replies
-
Exactly, where is your foreign key in this code, without that you cannot get values return in your select list
Beta Was this translation helpful? Give feedback.
All reactions
-
The database that EF scaffolded doesn't have foreign keys. Can I add the foreign keys in the model after the scaffold?
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes you can by using core command, dotnet ef migrations add key
Beta Was this translation helpful? Give feedback.