【发布时间】:2011-08-10 23:13:11
【问题描述】:
我们正在使用 EF 4.1 和 fluent API 从旧数据库(我们不允许更改)获取数据。我们在创建两个表之间的关系时遇到问题,其中相关列是非主键和外键。
使用下面的类,我们如何配置Report 和RunStat 之间的一对多关系,以便Report.RunStats 将返回ReportCode 字段相等的所有RunStat 实体?
public class Report
{
[Key]
public int ReportKey { get; set; }
public string Name { get; set; }
public int ReportCode { get; set; } // Can we associate on this field?
public virtual ICollection<RunStat> RunStats { get; set; }
}
public class RunStat
{
[Key]
public int RunStatKey { get; set; }
public int ReportCode { get; set; }
public DateTime RunDate { get; set; }
}
基本上,我想使用 Fluent API 配置 EF,使其将 Report.ReportCode 视为外键,将 RunStat.ReportCode 视为主键。
【问题讨论】:
标签: c# entity-framework foreign-keys primary-key entity-framework-4.1