0
 public class hotelapiController : ApiController
{
    private POSDBEntities db = new POSDBEntities();


    public IList<HotelsDetailsDto> GetHotels()
    {
        return db.Hotels.Select(p => new HotelsDetailsDto

        {
            Id = p.Id,
            Name = p.Name,
            Address = p.Address,
            Description = p.Description,
            Offering = p.Offering,
            Gps = p.Gps,
            NumberOfRooms = p.NumberOfRooms,
            Commission = p.Commission,
            Rating = p.Rating,    
            HotelTypeName=p.HotelTypeName,
            HimageId = p.HimageId,          //HimageId is a foreign key
            AveragePrice=p.AveragePrice,
            BookingEmail =p.BookingEmail,
            LocationId =p.LocationId,      //LocationId is a foreign key               
          }).ToList();
    }

When postman returns the JSON data it returns only the ids of the foreign key. But I need it to return data in the location table ie Name and Image of the Location not only the Location Id. Im using data transer objects and Database First approach not code first. How do i go about i?

s952163
  • 6,134
  • 4
  • 21
  • 44
Ransana
  • 1
  • 3

2 Answers2

0

If you have correctly defined your foreign keys in the database, then (in my experience) Entity Framework should have identified this when you asked it to create the model from the database, and provided you with the linked entities within the model. e.g. your "Hotel" should have a "Location" property. So you should already be able to write something like:

public IList<HotelsDetailsDto> GetHotels()
{
    return db.Hotels.Select(p => new HotelsDetailsDto
    {
        Id = p.Id,

        //removed other lines for brevity

        LocationName = p.Location.Name,
        LocationImage = p.Location.Image
  }).ToList();
}

If this is not the case, then define the database foreign keys between the tables correctly and then update your Entity Framework model. It should then provide you access to the entities you need.

ADyson
  • 44,946
  • 12
  • 41
  • 55
  • Thnks for your suggestion. but its tells me HotelsDetailsDto does not contain a defination for LocationName same as LocationImage – Ransana Jul 20 '16 at 11:58
  • @Ransana Did you read the last part of the answer? Have you defined relationships between Hotel and Location in your database? – ADyson Jul 20 '16 at 12:36
0

It is unclear what EF version are you using, but it seems, that you want to eager load your Hotels object. You can do this by reading article provided or this stackoverflow answer Entity framework linq query Include() multiple children entities

And it should like this:

 public IList<HotelsDetailsDto> GetHotels()
        {
            return db.Hotels.Include(x => x.Himage).Include(x => x.Location)
.Select(p => new HotelsDetailsDto
            {
                Id = p.Id,
                Name = p.Name,
                // Other fields
                HimageId = p.Himage.Id,          //HimageId is a foreign key
                LocationId =p.Location.Id,      //LocationId is a foreign key               
              }).ToList();
Community
  • 1
  • 1
Mantas Marcinkus
  • 563
  • 2
  • 5
  • 11