我试图在另一个对象中填充一个对象,同时避免两件事:
- 获取表中的所有项目
- 为每个项目单独调用数据库
这是我的代码
课程
public class Profile: BaseEntity
{
public Picture Picture { get; set; }
public string Name { get; set; }
}
public partial class Picture : BaseEntity
{
public string Name {get; set;}
}
服务
public class ProfileService : IProfileService
{
private readonly IRepository<Profile> _profileRepo;
private readonly IRepository<Picture> _pictureRepo;
public ProfileService(IRepository<Profile> profileRepo, IRepository<Picture> pictureRepo)
{
_profileRepo = profileRepo;
_pictureRepo = pictureRepo;
}
public IEnumerable<Profile> GetProfiles()
{
IEnumerable<Profile> profiles = _profileRepo.Table.ToList();
// I am guessing I would retrieve pictures here based off of picture
// ids that are in the profiles
// than I would populate the profiles with the pictures
}
}
同样在数据库 PictureId 是 Profile 表中的外键。