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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:33:05+00:00 2026-06-04T14:33:05+00:00

I have a scenario where I need to bind two properties from one class

  • 0

I have a scenario where I need to bind two properties from one class to another class using Entity Framework 4.1 Code-First. (For reference, this model is being consumed by Upshot.js for use in a single page application using Knockout.js 2.1)

Normally, I would have done something like the following:

    public class Person
    {
        [Key]
        public int PersonId { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Address HomeAddress { get; set; }
        public Address OfficeAddress { get; set; } 
    }

    public class Address
    {
        [Key]
        public int AddressId { get; set; }

        public string StreetAddress { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
    }

This allows HomeAddress and OfficeAddress to both reference an instance of the Address class.

Please note this is not my real data model. It is for illustrative purposes only. In real life, I probably would have used an ICollection. Unfortunately, it is not feasible for this particular situation, and I do need to maintain multiple references from one class to another.

In this particular scenario, it is also possible for a person to exist without any addresses defined. It is also possible for addresses to exist without a person. (As mentioned, this data model is just an example.)

While this compiles correctly, and I can even create and save data, upshot complains bitterly when it attempted to use this model.

It gives an (inner) exception like the following:

{"Unable to retrieve association information for association 'KnockoutTest.Models.Person_HomeAddress'. Only models that include foreign key information are supported. See Entity Framework documentation for details on creating models that include foreign key information."}

So… I attempted to set the Foreign Key information on the DbContext class like

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Person>()
    .HasOptional(m => m.HomeAddress);

    modelBuilder.Entity<Person>()
    .HasOptional(m => m.OfficeAddress);
}

No joy! This still gave me the same exception.

I am not sure how to set up the foreign key association so that multiple properties on one class can reference another class–at least not in a way that upshot will be pleased and stop complaining for a while.

What am I doing wrong, and how do I fix it?

  • 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-04T14:33:06+00:00Added an answer on June 4, 2026 at 2:33 pm

    You just have to introduce scalar foreign key properties into your model class:

    public class Person
    {
        [Key]
        public int PersonId { get; set; }
    
        public string FirstName { get; set; }
        public string LastName { get; set; }
    
        [ForeignKey("HomeAddress")]
        public int? HomeAddressId { get; set; }
        public Address HomeAddress { get; set; }
    
        [ForeignKey("OfficeAddress")]
        public int? OfficeAddressId { get; set; } 
        public Address OfficeAddress { get; set; } 
    }
    

    The properties must be nullable (int?) because your relationships are optional (person can exist without addresses).

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

Sidebar

Related Questions

I have a scenario where I need to search from many binary files (using
we are using Struts2 in our code and I have scenario where I need
I have a scenario where I need to upload a file from one web
I have a scenario where I need to pull the title from a img
Scenario. I have a client with two network connections to a server. One connection
I have a scenario where I need to display charts(generated using jfreecharts) converted it
I have a scenario where I need to join two SQL tables and am
I have a scenario where I need to do the following in a transaction:
I have a scenario where I need a desktop console app to communicate with
I have a scenario where i need to copy the array of Objects(Main array)

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.