我们使用EF 4.1和流畅的API从遗留数据库中获取数据(我们不允许更改).我们在创建两个表之间的关系时遇到问题,其中相关列不是主键和外键.
使用下面的类,我们如何配置之间的一对多关系Report,RunStat以便Report.RunStats返回字段相等的所有RunStat实体ReportCode?
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 { …Run Code Online (Sandbox Code Playgroud) c# entity-framework foreign-keys primary-key entity-framework-4.1
目标是使用GravityZone中的所有字段以及来自Zone表的区域名称的API.我已经尝试了以下代码的几种排列而没有成功.它目前为区域提供null,我希望将该名称作为字符串或对象的一部分.我正在使用我无法修改的现有表.
楷模:
public partial class Zone
{
[Key]
[Column("ZONE_ID")]
public decimal ZoneId { get; set; }
[Column("ZONE_CODE")]
public decimal ZoneCode { get; set; }
[Column("ZONE_NAME")]
public string ZoneName { get; set; }
public virtual ICollection<GravityZone> GravityZones { get; set; }
}
public partial class GravityZone
{
[Key]
[Column("GRAVITY_ID")]
public decimal GravityZoneId { get; set; }
[Column("ZONE_CODE")]
public decimal ZoneCode { get; set; }
[Column("ELEVATION")]
public decimal Elevation { get; set; }
[Column("REMARK")]
[StringLength(2000)]
public string Remark { get; set; } …Run Code Online (Sandbox Code Playgroud)