1

我正在尝试使用 NPoco 的 fetchOneToMany 方法将对象映射到嵌套对象列表,如下所示

[TableName("sds_ingredients_ing")]
[PrimaryKey("ing_id")]
public class Ingredient
{
    [Column(Name = "ing_id")]
    public int Id { get; set; }
    [Column(Name = "ing_cas")]
    public string Cas { get; set; }
    [Ignore]
    public IList<IngredientLang> Ingredient_Lang;
}

[TableName("sds_ingredients_lang")]
[PrimaryKey("ing_id")]
public class IngredientLang
{
    [Column(Name = "ing_id")]
    public int Id { get; set; }
    [Column(Name = "lang_id")]
    public int LangId { get; set; }
    [Column(Name = "Name")]
    public string Name { get; set; }
}

这是查询:

List<Ingredient> list = db.FetchOneToMany<Ingredient, IngredientLang>(x => x.Id,
    @"SELECT     ing.*, 
                 lang.*
      FROM       SDS_INGREDIENTS_ING ing 
      LEFT JOIN  SDS_INGREDIENTS_LANG lang 
      ON         ing.ING_ID=lang.ING_ID");

Npoco 返回以下错误:No Property of type ICollection`1 found on object of type: Ingredient,这让我感到困惑,因为类 Ingredient 确实具有 IList 类型的属性。我们已经尝试过 List、IList、IEnumerable 以及几乎我们能想到的所有类型的集合,但它们都不起作用。

你知道可能出了什么问题吗?

4

2 回答 2

2

Ingredient_Lang不是属性。它是一个领域。如果你把它变成一个属性,它应该可以工作。

于 2014-08-06T12:30:35.477 回答
0
 public partial class Graph
 {
    [Reference(ReferenceType.Many, ColumnName = "GraphId", 
     ReferenceMemberName = "GraphId")]
    [Column] public List<Plot> Plots { get; set; }
...
[TableName("Graph")]
[PrimaryKey("GraphId")]
[ExplicitColumns]
public partial class Graph : fooDB.Record<Graph>  
{   
...

在逐步浏览 NPoco 3.2.0 的源代码后,我了解到我需要将 [Column] 放在 List 成员上,以便在调用 FetchOneToMany 后在我的类中引出预期的子父级:

results = tdb.FetchOneToMany<Graph>(g => g.Plots, _plotsByReview, reviewId);

此外,保持您的选择列与父列之后的子列连续:

private static string _plotsByReview = @"
        select 
        g.GraphId,
        g.GraphName,
        g.UserId,
        g.Shared,
        g.ReviewId,
        p.PlotId as Plots__PlotId,
        p.GraphId as Plots__GraphId,
        p.TagId as Plots__TagId,
        p.YAxis as Plots__YAxis,
        p.Relabel as Plots__Relabel,
        p.Scale as Plots__Scale,
        p.MinY as Plots__MinY,
        p.MaxY as Plots__MaxY
        from [Graph] g
        inner join [Plot] p on p.GraphId = g.GraphId
        where g.ReviewId = @0
    ";

NPoco 规则!!!

于 2016-05-05T17:27:01.220 回答