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 8639331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:58:27+00:00 2026-06-12T10:58:27+00:00

I am trying to define the following model where Appointment table has foreign key

  • 0

I am trying to define the following model where Appointment table has foreign key for Person, and the entities both have navigation properties to each other.

public class Appointment
{
    public int AppointmentId { get; set; }

    // Foreign Key property (this will be created in DB)
    public int? PersonId { get; set; }

    // Navigation property to Flatmate
    public virtual Person Person { get; set; }
}

public class Person
{
    public int PersonId { get; set; }

    // Just navigation property. Don't want Person table to include foreign key (no need)
    public virtual Appointment Appointment { get; set; }
}

I try to use fluent config:

     modelBuilder.Entity<Appointment>()
            .HasOptional(a => a.Person)
            .WithOptionalDependent(p=> p.Appointment);

But I get an exception that its missing a column (either Appointment_AppointmentId or Person_PersonId, depending on whether I use WithOptionalDependent or WithOptionalPrincipal).

  • 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-12T10:58:28+00:00Added an answer on June 12, 2026 at 10:58 am

    Entity Framework doesn’t support this. HasOptional().WithOptionalDependent() can work when both tables use the same key (PersonId == AppointmentId), but that’s not the situation you have. In order to ensure that one person doesn’t have multiple appointments, you’d need to make sure PersonId is unique in the Appointment table, and Entity Framework doesn’t support unique constraints.

    What you could do (without changing your database) is map this as a one-to-many relation, where one person can have multiple appointments, and create a helper property to return the one appointment:

    public virtual ICollection<Appointments> Appointments { get; set; }
    [NotMapped]
    public Appointment Appointment {
        get {
            return Appointments.SingleOrDefault();
        }
    }
    

    Note that Entity Framework won’t understand the Appointment property, so you cannot use it in queries.

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

Sidebar

Related Questions

I am trying to model the following table structure in Symfony 2.0 using annotations.
basically I am trying to model some map brushes that have the following format:
I am trying to do the following: #define mkstr(str) #str #define cat(x,y) mkstr(x ##
I'm trying to define a new shortcode in WordPress, and I get the following
I'm trying to load a hypothetical plugin with the following header: #ifndef _DLL_H_ #define
I am trying to understand the following code: #include<stdio.h> #include<stdlib.h> #include<sys/io.h> #define baseport 0x378
I am trying to define an abstract class that has operators to compare two
I am trying to figure out how to define a callback to my model
I'm trying to make the domain model of a management system. I have the
I'm working on my first Rails project, and I have the following model relationship:

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.