schotime / NPoco

Simple microORM that maps the results of a query onto a POCO object. Project based on Schotime's branch of PetaPoco
Apache License 2.0
847 stars 301 forks source link

One To Many References not working #682

Open deekoulos opened 1 year ago

deekoulos commented 1 year ago

Hi there, i googled a lot including chatgpt, none of it is working for me. I am trying to do a very simple one to many relation between 2 tables, and get the sequence contains no elements exception.

[TableName("ServiceProviders")]
[PrimaryKey("Id")]
[ExplicitColumns]
public class ServiceProvider
{
        [Column("ServiceProviderId")]
        public int Id { get; set; }

        [Reference(ReferenceType.Many, ColumnName = "ServiceProviderId", ReferenceMemberName = "MemberId")]
        public List<Test> Tests { get; set; }
}
[TableName("Tests")]
[PrimaryKey("Id", AutoIncrement = true)]
[ExplicitColumns]
public class Test
{
    [Column("TestId")]
    public int Id { get; }

    [Column("MemberId")]
    public int MemberId { get; set; }

    [Reference(ReferenceType.Foreign, ColumnName = "ServiceProviderId", ReferenceMemberName = "Id")]
    public ServiceProvider ServiceProvider { get; set; }
}

I also tried the following without success:

    [TableName("ServiceProviders")]
    [PrimaryKey("Id")]
    [ExplicitColumns]
    public class ServiceProvider
    {
        [Column("ServiceProviderId")]
        public int Id { get; set; }

        [Reference(ReferenceType.Many, ColumnName = "ServiceProviderId", ReferenceMemberName = "ServiceProvider")]
        public List<Test> Tests { get; set; }
    }
    [TableName("Tests")]
    [PrimaryKey("Id", AutoIncrement = true)]
    [ExplicitColumns]
    public class Test
    {
        [Column("TestId")]
        public int Id { get; }

        [Column("MemberId")]
        public int MemberId { get; set; }

        [Reference(ReferenceType.Foreign, ColumnName = "ServiceProviderId", ReferenceMemberName = "Id")]
        public ServiceProvider ServiceProvider { get; set; }
    }

what am i missing?

Best, Dee

deekoulos commented 1 year ago

I also copy pasted the fixed example provided here: https://github.com/schotime/NPoco/issues/315

still getting the sequence contains no elements error.