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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:55:47+00:00 2026-06-03T22:55:47+00:00

When creating many to many relationship using EF 4.3 code first approach, I cannot

  • 0

When creating many to many relationship using EF 4.3 code first approach, I cannot save data to connecting table, also cannot any examples on how to fill this table using saving object to Icollection… Here is my example:

MODELS

public class Hospital
{
    //PK
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public Guid User_Id { get; set; }

    //FK
    public virtual ICollection<Operator> Operators { get; set; }

}

public class Operator
{
    //PK
    [Key]
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Dob { get; set; }
    public string Email { get; set; }

    //FK
    public virtual ICollection<Hospital> Hospitals { get; set; }
}

public class Project: DbContext
{
    public DbSet<Hospital> Hospitals { get; set; }
    public DbSet<Operator> Operators { get; set; }
}

CONTROLLER

public void AddOperater()
{

    Hospital h = new Hospital();
    h = db.Hospitals.Single(a=>a.Id ==1);

    var o = new Operator();
    o.FirstName = "John";
    o.LastName = "Doe";
    o.Dob = new DateTime(1988,2,12);
    o.Email = "johndoe@gmail.com";
    o.Hospitals.Add(h);

    db.SaveChanges();
 }

With this approach I keep getting error here: o.Hospitals.Add(h); even when my Hospital instance is filled with data. How exactly to save data to both tables, the dbo.Operators and dbo.OperatorHospital which is relationship table?

  • 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-03T22:55:48+00:00Added an answer on June 3, 2026 at 10:55 pm

    o.Hospitals.Add(h) will fail because the list is a null list. You cannot call Add() on a null list. Typically most people get around this by instantiating the list in the constructor of the entity… like so… the current line is blowing up due to a CSharp issue.

    public class Hospital
    {
        //PK
        [Key]
        public int Id { get; set; }
    
        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string County { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public Guid User_Id { get; set; }
    
         //FK
        public virtual ICollection<Operator> Operators { get; set; }
    
        public Hospital()
        {
             Operators = new List<Operator>();
        }
    
    }
    
    
    public class Operator
    {
       //PK
       [Key]
       public int Id { get; set; }
    
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public DateTime Dob { get; set; }
       public string Email { get; set; }
    
       //FK
       public virtual ICollection<Hospital> Hospitals { get; set; }
    
       public Operator()
       {
           Hospitals = new List<Hospital>();
       }
    

    }

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

Sidebar

Related Questions

I'm struggling with creating a simple one-to-many relationship using Code First in EF. I
I'm trying to create a many-to-many relationship using EF code first in my MVC
how would i go about creating a many-many relationship among data objects in google
I'm creating a database using entity framework code first and I'm having some issues
I am creating an application which involves so many web-service calls. I am using
I'm creating a dynamically generated PDF using FPDF. My PDF requires many exactly horizontal/vertical
I am creating an iOS 5 app using Core Data. I have two entities
Using EF 4.1 Code First, I have a Member entity and it in turn
I'm having some difficulty creating nested objects between a one-to-many relationship. My models are
From the Rails associations guide, they demonstrate a many-to-many relationship using has_many :through like

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.