EntityFramework:如何映射Zero-to-One关系
本文关键字:映射 Zero-to-One 关系 何映射 EntityFramework | 更新日期: 2023-09-27 18:22:42
我有以下数据库表:
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)
以下是与上表相对应的我的实体:
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; }
}
现在,当我从数据上下文中获得FundAllocation列表时,其Allocation属性设置为null。实现这种零对一关系需要什么额外的配置?
要解决您的问题,您必须通过以下方式填充属性:
- 启用延迟加载:
public virtual Allocation Allocation { get; set; }
- 急切加载:
- 急切地装载