Can't return a view with include()
ASP.net MVC3 Razor EF
When I开发者_如何学Python do this it works:
public ViewResult Index()
{
return View(db.Songs.Include("Artist").ToList());
}
But this doesn't:
public ViewResult Index()
{
return View(db.Artists.Include("Song").ToList());
}
I get this error:
A specified Include path is not valid. The EntityType 'MVCProject.Models.Artist' does not declare a navigation property with the name 'Song'.
Any idea why? if you need more info/code please mention which. but please let me know where something like that can happen, and how it can be solved. it's driving me crazy.
Thanks.
Artists Class:
public class Artist
{
public int ArtistID{ get; set; }
public string Name { get; set; }
public IQueryable<Song> Songs{ get; set; }
}
Replace public IQueryable<Song> Songs{ get; set; }
with public virtual ICollection<Song> Songs{ get; set; }
public class Artist
{
public int ArtistID{ get; set; }
public string Name { get; set; }
public virtual ICollection<Song> Songs{ get; set; }
}
Then
return View(db.Artists.Include("Songs").ToList());
You probably have Songs (plural) as navigational property for Artist class. So, return View(db.Artists.Include("Songs").ToList());
should work. If ot - you need to show Model class for Artist
精彩评论