我正在调用一个似乎无法正常工作的linq语句:
我的客户端型号:
public class Client
{
[UIHint("Hidden")]
public int ClientId { get; set; }
[UIHint("Hidden")]
public int UserProfileId { get; set; }
public int Age { get; set; }
[UIHint("Hidden")]
public int GenderId { get; set; }
public string GenderDescription { get; set; }
[UIHint("Hidden")]
public int SettingId { get; set; }
public virtual Gender Gender { get; set; }
public virtual Setting Setting { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
我的Linq电话:
var clients = db.Clients.Include(c => c.Gender).Include(c => c.Setting).Include(c => c.UserProfile).OrderBy(c => c.ClientId);
这将使用GenderId和SettingId基于适当的信息填充Gender和Setting字段,但UserProfile不会。有没有合理的解释,还是我需要不停地靠在桌子上?
答案 0 :(得分:0)
我遇到的问题是,当我创建UserProfile模型时,我使用了UserId而不是UserProfileId。该框架一直在寻找一个不存在的Id。
正确的模型应如下所示:
public class UserProfile
{
public UserProfile()
{
this.Casefiles = new HashSet<Casefile>();
}
[Display(Name="Author")]
public string FullName
{
get { return LastName + ", " + FirstName; }
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserProfileId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[EmailAddress]
public string EmailAddress { get; set; }
// UserInRole Level
public string Role { get; set; }
[Display(Name = "Request Account Uplevel to Contributor")]
public bool RequestUplevel { get; set; }
public virtual ICollection<Casefile> Casefiles { get; set; }
}
当您拨打电话时这样:
var clients = db.Clients.Include(c => c.Gender)
.Include(c => c.Setting)
.Include(c => c.UserProfile)
.OrderBy(c => c.ClientId)
.Where(u => u.UserProfileId == WebSecurity.CurrentUserId);
这将返回客户端模型中的每个虚拟调用。