How can I map between tables using fields that aren't primary keys?

I have two tables defined as such:

CREATE TABLE [SomeTable] (
[PrincipalID] [int] IDENTITY(1,1) NOT NULL,
[AnotherUniqueField] [varchar](50) NOT NULL,
[Data] [varchar](50) NOT NULL,

CREATE TABLE [OtherTable] (
[OtherID] [int] NOT NULL,
[UniqueIfNotNullField] [varchar](20) NULL,
[OtherData] [nvarchar](50) NOT NULL,

These two tables record information on the same conceptual objects and I would like to be able to create a relationship between them in the EF entities I have for them, using SomeTable.AnotherUniqueField and OtherTable.UniqueIfNotNullField as the "keys."

SomeTable.AnotherUniqueField has a unique index but, obviously, is not the PK. OtherTable.UniqueIfNotNullField is, as indicated, unique when the value is not null. (This constraint isn't enforced in the database, but the source for the table's data is guarded to guarantee uniqueness when there is a value.)

Here's are the POCO entities:

public class SomeTable
    public int PrincipalID { get; set; }
    public int SameField { get; set; }
    public string Data { get; set; }
    public OtherTable Other { get; set; }

public class OtherTable
    public int OtherID { get; set; }
    public string SameField { get; set; }
    public string OtherData { get; set; }

And my EntityTypeConfiguration definitions for the same:

public class SomeTableMap : EntityTypeConfiguration<SomeTable>
    public SomeTableMap()
        HasKey(t => t.PrincipalID);
        Property(t => t.Data)
        Property(t => t.SameField)
        Property(t => t.PrincipalID).HasColumnName("PrincipalID");
        Property(t => t.SameField).HasColumnName("AnotherUniqueField");
        Property(t => t.Data).HasColumnName("Data");

public class OtherTableMap : EntityTypeConfiguration<OtherTable>
    public OtherTableMap()
        HasKey(t => t.OtherID);
        Property(t => t.OtherData)
        Property(t => t.SameField)
        Property(t => t.OtherID).HasColumnName("OtherID");
        Property(t => t.SameField).HasColumnName("UniqueIfNotNullField");
        Property(t => t.OtherData).HasColumnName("OtherData");

Is it possible to map this way? I have tried the following in the constructor for SomeTableMap, but it doesn't work. I'm at the end of my understanding of custom mappings.

HasOptional<OtherTable>(x => x.Other)
        map =>

Is this just not a mappable join? I have another option since OtherTable isn't really an editable entity; I can make a view which will include the associated value of SomeTable.PrincipalID, but I'd like to see if this will work first.

EDIT: I also tried this:

HasOptional(x => x.Other)
    .Map(map => map.MapKey("UniqueIfNotNullField"));

but I get the error: "Each property name in a type must be unique. Property name 'UniqueIfNotNullField' was already defined."


Entity framework do not support this relation. The core problem is missing support for Unique constraint in EF. So the key in principal entity always have to be a primary key and if you want to build one-to-one relation the foreign key in dependent entity must be also primary key.

Need Your Help

Google charts number range filter doesn't work properly with accordions

filter google-visualization range jquery-accordion jquery-ui-accordion

Google charts range filter works perfectly fine outside of accordion. The default range sliders are set at left most and right most. For example: