nPoco - Get single object with nested data

926 views Asked by At

I have two Dto's:

[TableName("Address")]
    public class AddressDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Street { get; set; }
        public string Building { get; set; }
        public string Appartment { get; set; }
        public string ZipCode { get; set; }
        public string Floor { get; set; }
        public DateTime CreatedDate { get; set; }
    }

[TableName("DistributionPoint")]
    public class DistributionPointDto
    {
        [Column("Id")]
        public int Id { get; set; }

        public string Name { get; set; }

        [Reference(ReferenceType.Foreign, ColumnName = "AddressId", ReferenceMemberName = "Id")]
        public AddressDto Address { get; set; }
    }

How can I get DistributionPointDto with nested AddressDto using nPoco? I have a generic repository for CRUD with method:

 public T FindById<T>(int id)
        {
           return _db.SingleById<T>(id);
        }

But, when I'm trying to get DistributionPointDto, AddressDto is null

1

There are 1 answers

0
Kelly On

Curious if you got your code to work, but I have found that you need to use Query instead of SingleById to include referenced properties, and then you can use an Include statement/clause, however I don't think generics work with this, you have to explicitly mention the referenced property name, which is AddressDTO for you.

_db.Query<DistributionPointDTO>()
  .Include(x => x.AddressDTO)
  .Where(x => x.Id == id).First();