我为此编写了一个静态扩展,因此我可以像这样在我的属性中设置它(使用我的一些类)。此属性将在您的 POCO 模型中:
private List<AssessmentAssignment> _AssessmentAssignments;
[ResultColumn]
public List<AssessmentAssignment> AssessmentAssignments
{
get
{
return ServiceExtensions<AssessmentAssignment>.GetRelatedList(this.GetType());
}
set
{
_AssessmentAssignments = value;
}
}
这是我为此编写的 ServiveExtension,但不确定我是否喜欢这个。它有效,但是嗯。有人有更好的想法吗?
public static class ServiceExtensions<T> where T : class, new()
{
/// <summary>
/// Experimental way to retrieve a foreign key related list with PetaPoco
/// </summary>
/// <param name="PrimaryTableType"></param>
/// <returns></returns>
public static List<T> GetRelatedList(Type PrimaryTableType)
{
var primaryTableData = PocoData.ForType(PrimaryTableType);
var relationshipTableData = PocoData.ForType(typeof(T));
using (PetaPoco.Database db = new PetaPoco.Database("ConnStringName"))
{
PetaPoco.Sql sql = new PetaPoco.Sql("SELECT A.* FROM " + relationshipTableData.TableInfo.TableName + " A")
.Append("JOIN " + primaryTableData.TableInfo.TableName + " B ON A." + primaryTableData.TableInfo.PrimaryKey + " = B." + primaryTableData.TableInfo.PrimaryKey);
return db.Fetch<T>(sql);
}
}
}