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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:20:08+00:00 2026-06-04T05:20:08+00:00

I am using this tutorial from microsoft to create a one-zero-to-one relationship with EF4.1

  • 0

I am using this tutorial from microsoft to create a one-zero-to-one relationship with EF4.1 Between an Instructor and OfficeAssignment. This is working like a charm.

But now I want to add a Home for each Instructor (1 to zero-or-1) like in this:

https://i.stack.imgur.com/ZmYur.png

I added the Home model exactly the same way as the OfficeAssignment (like in the tutorial above), but when I try to add controllers for these model, I get the error “An item with the same name has already been added”.

So my model is set up incorrectly.
What is wrong with the below?
How do I create multiple one-to-zero-to-one relationships in EF4.1?

public class Instructor
{
    public Int32 InstructorID { get; set; }
    public string LastName { get; set; }
    public string FirstMidName { get; set; }
    public virtual OfficeAssignment OfficeAssignment { get; set; }
    public virtual Home Home { get; set; }
}

public class OfficeAssignment
{
    [Key]
    public int InstructorID { get; set; }
    public string Location { get; set; }
    public virtual Instructor Instructor { get; set; }
}

public class Home
{
    [Key]
    public int InstructorID { get; set; }
    public string Location { get; set; }
    public virtual Instructor Instructor { get; set; }
}

public class Context : DbContext
{
    public DbSet<OfficeAssignment> OfficeAssignments { get; set; }
    public DbSet<Instructor> Instructors { get; set; }
    public DbSet<Home> Homes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

          modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
          modelBuilder.Entity<Instructor>()
            .HasOptional(p => p.OfficeAssignment)
            .WithRequired(p => p.Instructor);
          modelBuilder.Entity<Instructor>()
             .HasOptional(p => p.Home).WithRequired(p => p.Instructor);
}
  • 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-04T05:20:09+00:00Added an answer on June 4, 2026 at 5:20 am

    Doesn’t look like EF supports real 1 to 0..1 relationship. You need a foreign key. And add the optional (int?) into the main model.

    So I did this as follow, and it works like a charm.

    public class Instructor
    {
        public Int InstructorID { get; set; }
        public string LastName { get; set; }
        public string FirstMidName { get; set; }
    
        public int? OfficeAssignmentID { get; set; }
        public virtual OfficeAssignment OfficeAssignment { get; set; }
    
        public int? HomeID { get; set; }
        public virtual Home Home { get; set; }
    
    }
    
    public class OfficeAssignment
    {
        public int OfficeAssignmentID { get; set; }
        public string Location { get; set; }
    
    }
    
    public class Home
    {
        public int HomeID { get; set; }
        public string Location { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this tutorial to create a login form http://www.ryancoughlin.com/2008/11/04/use-jquery-to-submit-form/ It authenticates against
I am following this tutorial on using the client-object model. And just starting from
I've used the exact files from this tutorial: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/ The ideea is to submit
Using this tutorial at section 5.5, I attempted to Assign the 'celsius' string attribute
I'm using this tutorial to add a Login Item to my application. Most of
I am using this tutorial to try to replace the default TitleBar with a
I built a Jquery dropdown menu using this tutorial . It works across browsers
I just installed Glassfish on my Ubuntu server (No GUI) using THIS tutorial. Everything
I'm trying to get Django running on GAE using this tutorial . When I
Android newbie here. I'm using this tutorial to show some rows that I fetch

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.