Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Razor Page SelectList not populating when using ViewModel #26228

Unanswered
kmichelle7601 asked this question in Q&A
Discussion options

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

You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

Exactly, where is your foreign key in this code, without that you cannot get values return in your select list

You must be logged in to vote
2 replies
Comment options

The database that EF scaffolded doesn't have foreign keys. Can I add the foreign keys in the model after the scaffold?

Comment options

Yes you can by using core command, dotnet ef migrations add key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /