Ellesedil
Ellesedil

Reputation: 1626

How to define references when one poco/table has a composite primary key

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

However, let's say that my CustomerAddress poco class actually has to be defined like this because someone decided to design the table like this a long, long time ago.

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

    [PrimaryKey]
    public string Id_2 { get; set; }
}

How can I properly define my [Reference] for the PrimaryAddress property in the Customer class with the composite key defined in CustomerAddress?

Upvotes: 0

Views: 143

Answers (1)

labilbe
labilbe

Reputation: 3584

You can't using APIs that rely on it (but you can still use SELECT) Please see OrmLite limitations.

I had same problem with a legacy database I can't modify (because another project cohabit with mine). So I deleted primary key then created a new Id field (autoincrement) on the database (PK) and finally created a unique constraint (NOT NULLABLE) on both fields.

So now, I can use OrmLite to select properly without breaking compatibility with the other project.

Upvotes: 1

Related Questions