这是我第一次使用 Entity Framework Code First,我正在尝试这样做toString()
:
public class User
{
public int UserId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Email { get; set; }
public string InterestIn { get; set; }
public virtual ICollection<User> Friends { get; set; }
public virtual ICollection<User> FromWhomIsFriend { get; set; }
public override string ToString()
{
string output = string.Empty;
output = String.Format("UserId: {0}, Name: {1}, Age: {2}, City: {3}, " +
"Country: {4}, Email: {5}, InterestIn: {6}, Friends: {7}",
UserId, Name, Age, City, Country, Email, InterestIn, Friends);
return output;
}
}
当我这样做时,toString()
我收到此错误:
'System.Data.EntityCommandExecutionException'
"There is already an open datareader associated with this command you must close it first."
我在这里做:
private static void showUsers()
{
using (var context = new AdnLineContext())
{
var users = from u in context.Users
select u;
foreach (User user in users)
{
Console.WriteLine(user.ToString());
}
}
}
我不明白,为什么我会得到它?我该如何解决?