aidrivenpost
aidrivenpost

Reputation: 1943

Petapoco - Can't auto join

I have an object call Country and another call CounytryRegions as express below

[TableName("Countries"),
PrimaryKey("Code", autoIncrement = false)]
public class Country
{
    public Country()
    {
        this.CountryRegions = new List<CountryRegion>();
    }

    public string Code { get; set; }
    public string Name { get; set; }

    [Foliaco.DataAccess.ResultColumn()]
    public List<CountryRegion> CountryRegions { get; set; }

}

[TableName("CountryRegions"),
PrimaryKey("Code", autoIncrement = false)]
public class CountryRegion 
{
    public CountryRegion()
    {
        this.Country = new Country();
    }

    public string Code { get; set; }
    public string Name { get; set; }

    public string CountryCode { get; set; }

    [Foliaco.DataAccess.ResultColumn()]
    public Country Country { get; set; }

}

If I execute the sql below

 Country country = this.DB.Fetch<Country,CountryRegion>("select * from dbo.countries c join dbo.countryregions cr on c.Code = cr.CountryCode where c.Code = @0","US").FirstOrDefault();

I'm expecting to get on the CountryRegions property of the Country class all the associated records to that country, but instead I getting the get following Error

Can't auto join CountryRegion

Thanks for the help

Upvotes: 1

Views: 1886

Answers (2)

Sam
Sam

Reputation: 42417

I just ran into this error using the NPoco library, which is based on PetaPoco.

The problem in my case was I was calling .Fetch instead of .FetchOneToMany.

Upvotes: 1

aidrivenpost
aidrivenpost

Reputation: 1943

I think I found the problem according with their documentation I need to crated a Realtor Class to load the list refer to section One to Many on the Petapoco Documentation, I've also found this helper class OneTOMany that makes the Realtor class more generic

Upvotes: 2

Related Questions