如果我尝试先使用代码,用户类:
public class User
{
[Key]
public Guid ID { get; set; }
[Required(ErrorMessage = "You must pick a user name")]
[DisplayName("User Name")]
public string Username { get; set; }
[Required]
public string FullName { get; set; }
[Required(ErrorMessage = "You give a valid email address")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
public DateTime CreateAt { get; set; }
public string Image { get; set; }
public bool RemeberMe { get; set; }
public bool Dummy { get; set; }
public virtual ICollection<UserFriend> Friends { get; set; }
}
UserFriend 类: - 持有用户之间的关系
public class UserFriend
{
[Key, Column(Order = 0), ForeignKey("User")]
public Guid UserId { get; set; }
public User User { get; set; }
[Key, Column(Order = 1), ForeignKey("Friend")]
public Guid FriendId { get; set; }
public User Friend { get; set; }
public DateTime DateCreated { get; set; }
}
用户存储库:
public class UserRepository : IUserRepository
{
public User GetUserByName(string userName)
{
return context.Users.FirstOrDefault(u => u.Username == userName);
}
}
如果我尝试使用这种方法:
var userRepo = new UserRepository(new DataContext());
User user = userRepo.GetUserByName(User.Identity.Name);
问题:如果我做对了,user.Friends.first().Friend 返回 null 我的延迟加载不起作用。
请帮忙...