0

我试图在另一个对象中填充一个对象,同时避免两件事:

  1. 获取表中的所有项目
  2. 为每个项目单独调用数据库

这是我的代码

课程

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 表中的外键。

4

1 回答 1

0

如果对象之间存在关系,那么您应该能够在存储库中创建一个方法来执行以下操作:

public IEnumerable<Profile> GetAllProfilesWithPictures()
{
    return your_context.Profiles.Include(x => x.Picture);
}

这将返回所有配置文件及其图片。

不太清楚你说Getting all items in table.. 时的意思。如果你想限制你得到的东西,那么你可以在上面的语句中放置一个 where 子句或其他任何东西。

于 2013-07-15T07:50:58.707 回答