Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8405421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:50:59+00:00 2026-06-09T22:50:59+00:00

I have two tables defined as such: CREATE TABLE [SomeTable] ( [PrincipalID] [int] IDENTITY(1,1)

  • 0

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.“

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:51:00+00:00Added an answer on June 9, 2026 at 10:51 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: CREATE TABLE [NEWS] ( [ID] INT IDENTITY(1,1) NOT NULL, [TITLE]
I have the following two tables defined... CREATE TABLE [LogLevel] ( [Id] int primary
I have two tables A and B as defined bellow. create table A (
I have two tables defined as the following: User (ID int, name varchar) Hours
I have two classes defined as such: public class Questionnaire { public int QuestionnaireID
I have two tables: Table Author: ID (INTEGER), author_name (VARCHAR), first_name (VARCHAR), last_name (VARCHAR),
I have two tables: table a ida valuea 1 a 2 b 3 c
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables, a user table and a application table: User id username

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.