相关疑难解决方法(0)

使用Entity Framework 4.1 Fluent API在非主键字段上创建关联

我们使用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

15
推荐指数
1
解决办法
1万
查看次数

实体框架6.1.3将外键映射到非主键

目标是使用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)

c# entity-framework-6

9
推荐指数
1
解决办法
6677
查看次数