c# - nPoco - 使用嵌套数据获取单个对象
问题描述
我有两个 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; }
}
如何使用 nPoco 获得带有嵌套 AddressDto 的 DistributionPointDto?我有一个用于 CRUD 的通用存储库,方法如下:
public T FindById<T>(int id)
{
return _db.SingleById<T>(id);
}
但是,当我尝试获取 DistributionPointDto 时,AddressDto 是null
解决方案
好奇你的代码是否可以工作,但我发现你需要使用 Query 而不是 SingleById 来包含引用的属性,然后你可以使用 Include 语句/子句,但是我不认为泛型适用于此,你必须明确提及引用的属性名称,即为您的 AddressDTO。
_db.Query<DistributionPointDTO>()
.Include(x => x.AddressDTO)
.Where(x => x.Id == id).First();
推荐阅读
- android - 有没有办法通过警报对话框的按钮完成当前活动?
- java - XML SOAP 信封与 JAXB 一起为空
- java - 如何通过命令行使用 TestNG 和标签触发 Cucumber 并行测试执行?
- objective-c - 如何将 id 类型的对象转换或设置为另一种类型,如 UIImageView 或 UIView
- mongodb - 如果我使用提示,为什么 Mongodb 只会使用我的索引
- python - 为什么绘图和保存的图像(字体大小、线宽)之间存在差异?
- c# - 是否可以在 C# 中编写基于规则的迭代器结构的二维数组(对于网格中的瓦片的邻居)?
- reactjs - 当此查询出错时,MobX 会显示上一个查询的结果
- python - 从包中获取类
- ios - 如何使用 TestFlight 测试 Firebase 动态链接(未发布的 ios 应用)