实体框架 6.1.3 将外键映射到非主键 [英] Entity Framework 6.1.3 Mapping Foreign key to non primary key

查看:26
本文介绍了实体框架 6.1.3 将外键映射到非主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是拥有一个 API,其中包含来自 GravityZone 的所有字段,以及来自 Zone 表的区域名称.我已经尝试了以下代码的几种排列但没有成功.它目前正在为 Zone 提供 null ,我希望将名称作为字符串或对象的一部分.我正在处理无法修改的现有表.

The goal is to have an API with all the fields from the GravityZone with the name of the zone coming from the Zone table. I've tried several permutations of the following code without success. It's currently coming up with null for the Zone which I'm hoping to get either the name as a string or part of the object. I'm working with existing tables that I'm not able to modify.

型号:

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; }

    public virtual Zone Zone { get; set; }
}

上下文(仅关系部分)

modelBuilder.Entity<Zone>()
    .HasKey(e => e.ZoneCode);

modelBuilder.Entity<GravityZone>()
    .HasRequired(e => e.Zones);

除了这部分,其他一切都很好:

Everything else comes back great except for this part:

"Zones":null,

推荐答案

现在可以在 Entity Framework 7(即 EF Core 1.0)中实现.

This is now possible in Entity Framework 7 (that is, EF Core 1.0).

来自 .Net Entity Framework 用户语音唯一约束(即候选键)支持:

在 EF Core 1.0 中添加了对此功能的支持,我们没有计划在 EF6 代码库中添加它.

Support for this feature was added in EF Core 1.0 and we don’t have plans to add it in the EF6 codebase.

这篇关于实体框架 6.1.3 将外键映射到非主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆