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

  • Home
  • SEARCH
  • 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 9176033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:00:57+00:00 2026-06-17T17:00:57+00:00

I am trying to model what I think is quite a basic scenario, but

  • 0

I am trying to model what I think is quite a basic scenario, but I can’t get my head around the correct way to define it in EF Code First 5.

I have three classes:

Employee
Postal Address
Contact Telephone Number

The rules are thus:

  • A Postal Address can be standalone
  • A Contact Telephone Number can be standalone
  • Optionally, an Employee can have a “Home” Postal Address
  • An Employee can have zero or more Contact Telephone Numbers

If I model this in SQL, I end up with four tables;

  • PostalAddress
  • ContactTelephoneNumber
  • Employee
  • EmployeeContactTelephoneNumber (bridge table)

By looking through the answers here on SO, I can produce something similar to my SQL model in Code First with the exception that I have to have an Employee navigation property on PostalAddress and an Employees navigation property on ContactTelephoneNumber. This goes against my business rules, because neither PostalAddress nor ContactTelephoneNumber will always be refered to be an Employee. For example, I could later add a Premises class which would also have a PostalAddress.

By way of code examples, what I have now resembles the following:

public class Employee
{
    public int EmployeeID {get;set;}
    public virtual PostalAddress? HomeAddress {get;set;}
}

public class PostalAddress
{
    public int PostalAddressID {get;set;}
    public string Address {get;set;} // It's not actually a string - this is for brevity!
}

public class ContactTelephoneNumber
{
    public int ContactTelephoneNumberID {get;set;}
    public string TelephoneNumber {get;set;} // It's not actually a string - this is for brevity!
}

I’m trying to define my relationships using the Fluent API rather than Annotations, to ensure my presentation layer remains unaware of Entity Framework when consuming these classes. My mapping currently resembles the following – is this “correct”?

public class EmployeeMap : EntityTypeConfiguration<Employee>
{
    public EmployeeMap()
    {
        this.HasOptional(e => e.HomeAddress).WithOptionalDependent(p => p.Value).Map(m     => m.MapKey("PostalAddressID"));
    }
}

This doesn’t compile; I get:

The type ‘PostalAddress?’ must be a reference type in order to use it as parameter ‘TTargetEntity’ in the generic type or method ‘System.Data.Entity.ModelConfiguration.EntityTypeConfiguration.HasOptional(System.Linq.Expressions.Expression>)’

Additionally, I don’t like that I’m passing “PostalAddressID” as a string constant into the call to MapKey().

Please could someone highlight the error of my ways? I’ve been searching for the past 3 hours to no avail!

Edit: I should mention that once I understand this part I’ll try and address the Employee>ContactTelephoneNumbers situation separately.

  • 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-17T17:00:57+00:00Added an answer on June 17, 2026 at 5:00 pm

    You have declared HomeAddress as a value type, specifically as a Nullable<T>.

    Remove the ? from the declaration (reference types allow null, and therefore do not need to be wrapped in the Nullable struct).

    public virtual PostalAddress HomeAddress {get;set;}
    

    To specify the mapping, I think you can use this:

    this.HasOptional( e => e.HomeAddress ).WithMany().HasForeignKey( e => e.HomeAddressID );
    

    You will also need to add the HomeAddressID property to the Employee class.

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

Sidebar

Related Questions

I'm trying to model this constraint in GMPL. As far as I can understand,
I'M quite new to this EF but I think I'M making progress. Anyway, it
I'm trying to use code first model for my simple blog site, I'm used
I'm trying to achieve a very basic goal that used to be quite simple
Good day guys, I've got quite a specific data model that I'm trying to
So I've search all around trying to figure this out, and I think I'm
I'm trying to model a class that has a method with a variable argument
I am trying to model a simple domain by using DDD. Database layer is
I'm trying to model bind a set of dynamically generated checkboxes so as to
I'm trying to model a asynchronous job processing framework using MailboxProcessor. My requirements are

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.