我要让这变得非常简单。我有两张桌子。
public class User
{
public virtual int UserId { get; set; } //primary key AND foreign key
//user attributes in here
public virtual UserProfile UserProfile { get; set; }
}
public class UserProfile
{
public virtual int UserId { get; set; } //primary key AND foreign key
//profile attributes in here
public virtual User User { get; set; }
}
基本上,它们是两个以 1-1 关系共享主键的表。这些是否应该合并为一个,我不知道,我是基于现有的数据库。
现在,我遇到的问题是我何时访问它。
这段代码运行得很快(第二个,也许是两个):
List<User> userList; //**This userList is defined elsewhere, but it's a list of about 400 users.
foreach (User user in userList)
{
var test = user.FirstName;
}
这段代码真的很慢(10-30 秒):
List<User> userList; //**This userList is defined elsewhere, but it's a list of about 400 users.
foreach (User user in userList)
{
var test = user.UserProfile.EmailAddress;
}
当我从用户表访问 UserProfile 时,为什么我的代码需要更长的时间?!