无法使用 include() 返回视图
ASP.net MVC3 Razor EF
当我这样做时,它起作用了:
public ViewResult Index()
{
return View(db.Songs.Include("Artist").ToList());
}
但这不起作用:
public ViewResult Index()
{
return View(db.Artists.Include("Song").ToList());
}
我收到此错误:
指定的包含路径无效。 EntityType“MVCProject.Models.Artist”未声明名为“Song”的导航属性。
知道为什么吗?如果您需要更多信息/代码,请注明。但请让我知道哪里可能发生类似的情况以及如何解决。这让我发疯。
谢谢。
艺术家类别:
public class Artist
{
public int ArtistID{ get; set; }
public string Name { get; set; }
public IQueryable<Song> Songs{ get; set; }
}
ASP.net MVC3 Razor EF
When I 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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
替换
public IQueryable;歌曲{获取;放; }
与public virtual ICollection;歌曲{获取;放;然后
Replace
public IQueryable<Song> Songs{ get; set; }
withpublic virtual ICollection<Song> Songs{ get; set; }
Then
您可能将歌曲(复数)作为 Artist 类的导航属性。因此,
return View(db.Artists.Include("Songs").ToList());
应该可以工作。如果 ot - 您需要显示艺术家的模型类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