Ellesedil
Ellesedil

Reputation: 1626

How can I eager load objects that are referencing the primary key of my POCO class in a 1:Many relationship?

Considering the documentation here, you can define foreign key relationships in your pocos like the given example:

public class Customer
{
    [References(typeof(CustomerAddress))]
    public int PrimaryAddressId { get; set; }

    [Reference]
    public CustomerAddress PrimaryAddress { get; set; }
}

This is fine, as there's a 1:1 relationship here. However, I have a 1:Many relationship I need to define, and the relationship is actually defined in the child object, not the parent object.

So, let's say I have these POCOs:

public class Customer
{
    [PrimaryKey]
    public int CustomerId { get; set; }

    public List<CustomerAddress> CustomerAddresses { get; set; }
}

public class CustomerAddress
{
    [PrimaryKey]
    public int CustomerAddressId{ get; set; }

    public int CustomerId { get; set; }
}

How can I have ORMLite eager load the CustomerAddresses property in the Customer POCO?

Upvotes: 0

Views: 66

Answers (1)

labilbe
labilbe

Reputation: 3584

You have to call Db.LoadSelect<Customer>() method and your customer(s) will retrieve CustomerAddresses (you need to add [Reference] attribute on top of your CustomerAddresses property).

Upvotes: 1

Related Questions