Entity Framework: 对象 returns 一开始是一个空列表,但突然列表被正确填充
Entity Framework: object returns with an empty list at first, but then suddenly the list is populated correctly
我有以下 class:
public class User
{
public int Id { get; set; }
public List<User> Connections { get; set; }
//other properties
public User()
{
Connections = new List<User>();
}
}
然后我有一个 DataContext class 用于存储:
public class DataContext : DbContext
{
public DataContext() { }
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
public virtual DbSet<User> Users { get; set; }
}
还有一个 UserService class:
public class UserService: IUserService
{
private DataContext _context;
public UserService(DataContext context)
{
_context = context;
}
public User GetById(int id)
{
return _context.Users.Find(id);
}
...
}
现在假设我正确地存储了 2 个用户,并且我将彼此添加到他们各自的连接列表中。
问题出在下面这段代码中:
var user1 = _userService.GetById(userId);
---> Here user1.Connections is an empty list (unexpected)
var results = anotherList.Select(x=>
{
---> Here user1.Connections have one object inside (the other user as expected)
});
我认为这是因为列表尚未填充,因为它从未被访问过,但我在控制器中的以下端点也有问题:
var userId = int.Parse(User.Identity.Name);
var user1 = _userService.GetById(userId);
var connectionsInfo = user1.Connections.Select(x => new
{
Id = x.Id,
//map other properties
});
return Ok(connectionsInfo);
//this time an empty list is returned in the response, instead of a list with a single object
我读到它可能与循环依赖有关,但我没有发现任何异常。
此外,我不明白为什么在一种情况下列表在之后填充,而在另一种情况下根本没有填充。
知道是什么原因造成的吗?
Also I do not understand why in one case the list is populated after and in the other case is not populated at all.
这是 entity framework 中的 Lazy Loading
功能。延迟加载意味着 延迟 相关数据的加载,直到您明确请求它。如需更多解释和深入了解,您可以查看 this good article。
Entity Framework supports three ways to load related data - eager loading, lazy loading, and explicit loading. for your scenario, It would prefer to use eager loading way. for achieving this goal EF has the Include()
方法。因此,您可以按如下方式更新 GetById
方法:
public User GetById(int id)
{
return _context.Users
.Include(item => item.Connections)
.Find(id);
}
使用上述查询,当您找到特定用户时,它的连接也会同时加载。祝你好运。
我有以下 class:
public class User
{
public int Id { get; set; }
public List<User> Connections { get; set; }
//other properties
public User()
{
Connections = new List<User>();
}
}
然后我有一个 DataContext class 用于存储:
public class DataContext : DbContext
{
public DataContext() { }
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
public virtual DbSet<User> Users { get; set; }
}
还有一个 UserService class:
public class UserService: IUserService
{
private DataContext _context;
public UserService(DataContext context)
{
_context = context;
}
public User GetById(int id)
{
return _context.Users.Find(id);
}
...
}
现在假设我正确地存储了 2 个用户,并且我将彼此添加到他们各自的连接列表中。
问题出在下面这段代码中:
var user1 = _userService.GetById(userId);
---> Here user1.Connections is an empty list (unexpected)
var results = anotherList.Select(x=>
{
---> Here user1.Connections have one object inside (the other user as expected)
});
我认为这是因为列表尚未填充,因为它从未被访问过,但我在控制器中的以下端点也有问题:
var userId = int.Parse(User.Identity.Name);
var user1 = _userService.GetById(userId);
var connectionsInfo = user1.Connections.Select(x => new
{
Id = x.Id,
//map other properties
});
return Ok(connectionsInfo);
//this time an empty list is returned in the response, instead of a list with a single object
我读到它可能与循环依赖有关,但我没有发现任何异常。
此外,我不明白为什么在一种情况下列表在之后填充,而在另一种情况下根本没有填充。
知道是什么原因造成的吗?
Also I do not understand why in one case the list is populated after and in the other case is not populated at all.
这是 entity framework 中的 Lazy Loading
功能。延迟加载意味着 延迟 相关数据的加载,直到您明确请求它。如需更多解释和深入了解,您可以查看 this good article。
Entity Framework supports three ways to load related data - eager loading, lazy loading, and explicit loading. for your scenario, It would prefer to use eager loading way. for achieving this goal EF has the Include()
方法。因此,您可以按如下方式更新 GetById
方法:
public User GetById(int id)
{
return _context.Users
.Include(item => item.Connections)
.Find(id);
}
使用上述查询,当您找到特定用户时,它的连接也会同时加载。祝你好运。