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

Entity Framework 6.1.3 Mapping Foreign key to non primary key(实体框架 6.1.3 将外键映射到非主键)
本文介绍了实体框架 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 将外键映射到非主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)