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,
CONSTRAINT [PK_SomeTable] PRIMARY KEY CLUSTERED ([PrincipalID] ASC))

CREATE TABLE [OtherTable] (
[OtherID] [int] NOT NULL,
[UniqueIfNotNullField] [varchar](20) NULL,
[OtherData] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_OtherTable] PRIMARY KEY CLUSTERED ([OtherID] ASC))

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()
    {
        ToTable("SomeTable");
        HasKey(t => t.PrincipalID);
        Property(t => t.Data)
            .IsRequired()
            .HasMaxLength(50);
        Property(t => t.SameField)
            .HasMaxLength(50);
        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()
    {
        ToTable("OtherTable");
        HasKey(t => t.OtherID);
        Property(t => t.OtherData)
            .IsRequired()
            .HasMaxLength(50);
        Property(t => t.SameField)
            .HasMaxLength(20);
        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)
    .WithOptionalPrincipal()
    .Map(
        map =>
            {
                map.MapKey("UniqueIfNotNullField");
                map.MapKey("AnotherUniqueField");
            });

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)
    .WithOptionalPrincipal()
    .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."

Answers


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: http://jsfiddle.net/samsumi007/1v5xyqL9/

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.