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

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

问题描述

目标是拥有一个来自GravityZone的所有字段的API,该区域的名称来自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<PressureZone>()
    .HasKey(e => e.PressureZoneCode);

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

除此之外,其他一切都很好,

Everything else comes back great except for this part:

Zones:null,

推荐答案

现在可能在实体框架7(即EF Core 1.0)中。

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

从.Net实体框架用户语音 唯一约束(即候选键)支持

From .Net Entity Framework User Voice Unique Constraint (i.e. Candidate Key) Support:


在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天全站免登陆