11

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.

Models:

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

Context (only the relational portion)

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,

Simon Hughes
  • 3,534
  • 3
  • 24
  • 45
stb
  • 113
  • 1
  • 6
  • Possible duplicate of [Create association on non-primary key fields with Entity Framework 4.1 Fluent API](https://stackoverflow.com/questions/7019052/create-association-on-non-primary-key-fields-with-entity-framework-4-1-fluent-ap) – DavidRR Nov 01 '18 at 14:14

2 Answers2

13

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

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

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

Note: the original page for this User Voice item is dead. However, a useful excerpt from the original can be seen in this related SO answer.

ErikE
  • 48,881
  • 23
  • 151
  • 196
  • Dang, that has been deleted – Traderhut Games Nov 28 '22 at 23:26
  • @TraderhutGames Sorry that happened. I'm a bit far from Entity Framework now... can you suggest a link that simply explains how to do this that I can update my answer with? – ErikE Nov 29 '22 at 00:20
  • @ErikE maybe this? https://stackoverflow.com/a/53102674/2881807 It has the original text included and seems to be the same as your original link. – LQd Jan 09 '23 at 09:39
0

I don't think it is possible cause it would cause a lot of trouble. Foreign key should should always point to some key of a table. I don't think you are able to tell EF about candidate keys.

Edit:

Similar Questions: Entity Framework 5.0 composite foreign key to non primary key - is it possible?

Here is answer for your question. As I thought EF does not understand the concept of uniqueness with exception of primary key.

Entity Framework foreign keys to non-primary key fields

Community
  • 1
  • 1
user2184057
  • 924
  • 10
  • 27
  • That's too bad. More grumbling will happen before I get this project sorted out. Thanks for the response! – stb May 07 '15 at 19:53