LoWii
LoWii

Reputation: 15

NPoco fetchOneToMany error

I am trying to use NPoco's fetchOneToMany method to map an object with a list of nested objects, following this link like so:

[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; }
}

And here is the query:

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 returns the following error: No Property of type ICollection`1 found on object of type: Ingredient, which confuses me because the class Ingredient does have a property of type IList. We've tried List, IList, IEnumerable and pretty much every type of collection we could think of and none of them worked.

Do you have any idea what might be going wrong?

Upvotes: 1

Views: 998

Answers (2)

Guy--L
Guy--L

Reputation: 170

 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>  
{   
...

After stepping through the source for NPoco 3.2.0, I learned that I needed to put [Column] on the List member to elicit the intended child-parent in my classes after invoking FetchOneToMany:

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

Also, keep your select columns contiguous with the child columns after the parent:

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 rules!!!

Upvotes: 0

Schotime
Schotime

Reputation: 15987

Ingredient_Lang is not a property. its a field. If you make it a property it should work.

Upvotes: 2

Related Questions