我有MVC框架用户表和我自己的社区表。我还有一个关系 CommunityUser 表。我实际上将CommunityUser表与社区表连接起来。 这是我的社区类
public class Community
{
[Key]
public int CommunityID { get; set; }
[Required]
public string CommunityName { get; set; }
[Required]
public string CommunityDomain { get; set; }
public string CommunityAbout { get; set; }
public string CommunityLogo { get; set; }
public int PrivacyID { get; set; }
public string CommunityAdmin { get; set; }
public virtual PrivacyLevel PrivacyLevel { get; set; }
public virtual ICollection<CommunityUser> CommunityUsers { get; set; }
public Community()
{
CommunityUsers = new Collection<CommunityUser>();
}
}
我想在索引页面上使用用户表和社区用户表。我创建了一个包含这两个表的 ProfileDataViewModel 。
public class ProfileDataViewModel
{
public ApplicationUser User { get; set; }
public List<CommunityUser> CommunitiesList { get; set; }
}
现在,当我在索引函数上使用此视图模型时,它会抛出异常。这是我的索引功能
public ActionResult Index()
{
var thisUser = db.Users.Where(u => u.UserName == User.Identity.Name).SingleOrDefault();
var myDashboard = new ProfileDataViewModel()
{
User = thisUser,
CommunitiesList = db.CommunityUsers.Include(x => x.Community).Where(u => u.UserID==thisUser.Id).ToList()
};
return View(myDashboard);
}