简体   繁体   English

nPoco - 获取具有嵌套数据的单个对象

[英]nPoco - Get single object with nested data

I have two Dto's:我有两个 Dto:

[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?如何使用 nPoco 获得带有嵌套 AddressDto 的 DistributionPointDto? I have a generic repository for CRUD with method:我有一个带有方法的 CRUD 通用存储库:

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

But, when I'm trying to get DistributionPointDto, AddressDto is null但是,当我尝试获取 DistributionPointDto 时,AddressDto 为null

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.好奇你的代码是否可以工作,但我发现你需要使用 Query 而不是 SingleById 来包含引用的属性,然后你可以使用 Include 语句/子句,但是我认为泛型不适用于这个,你必须明确提及引用的属性名称,即 AddressDTO。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM