gpt4 book ai didi

c# - Entity Framework 6.1.3 将外键映射到非主键

转载 作者:太空狗 更新时间:2023-10-29 17:46:00 26 4
gpt4 key购买 nike

目标是拥有一个 API,其中包含来自 GravityZone 的所有字段以及来自 Zone 表的区域名称。我已经尝试了以下代码的几种排列但没有成功。它目前为区域提出了 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; }

public virtual Zone Zone { get; set; }
}

上下文(仅关系部分)

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

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

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

"区域":null,

最佳答案

这在 Entity Framework 7(即 EF Core 1.0)中现在是可能的。

来自 .Net Entity Framework User Voice 项目“唯一约束(即候选键)支持”

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

注意:此 User Voice 项目的原始页面已失效。然而,可以在 this related SO answer 中看到有用的原文摘录。 .

关于c# - Entity Framework 6.1.3 将外键映射到非主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30037798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com