Lucifer
Lucifer

Reputation: 2357

EntityFramework: How to map a Zero-to-One relationship?

I have the below database tables:

FundAllocation {below two columns make a composite primary key}
       [CurrencyRefId] (int)  {FK to a table Currency.CurrencyId}
       [AllocationRefId] (int) {FK to Allocation.AllocationId}

Allocation
       [AllocationId] (int) {PK Identity Column}
       [AllocationTitle] (varchar)

Below are my entities corresponding to the above table:

public class FundAllocation
{
    [Key]
    [Column(Order = 1)]
    public int CurrencyRefId { get; set; }
    [Key]
    [Column(Order = 2)]
    public int AllocationRefId { get; set; }

    [ForeignKey("AllocationRefId")]
    public Allocation Allocation { get; set; }
}

public class Allocation
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AllocationId { get; set; }
    public string AllocationTitle { get; set; }
}

Now when I get the FundAllocation list from the data context, its Allocation property is set to null. What additional configuration is needed to achieve this Zero-to-One relationship ?

Upvotes: 0

Views: 47

Answers (1)

tschmit007
tschmit007

Reputation: 7800

To solve your problem you must populate the property by:

  • enabling lazy loading:
    • public virtual Allocation Allocation { get; set; }
  • eager loading :

Upvotes: 1

Related Questions