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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:02:55+00:00 2026-06-06T21:02:55+00:00

I have an object Customer that can have multiple customer types and each customer

  • 0

I have an object Customer that can have multiple customer types and each customer type can have multiple customers. I’m new to EF but have managed to add a customer, but can’t seem to get the syntax right for adding the customer’s customer types as well.

My customer class (simplified):

public partial class Customer
{
        public virtual int Id { get; set;}
        public virtual string Name { get; set;}

    #region Navigation Properties

        public virtual ICollection<CustomerType> CustomerTypes
        { get; set; }

    #endregion
}

Customer type:

public partial class CustomerType
{

        public virtual int Id
        {
            get;
            set;
        }

        public virtual string Name
        {
            get;
            set;
        }

        #region Navigation Properties

        public virtual ICollection<Customer> Customers
        { get; set; }

        #endregion
}

When I run this project a CustomerTypeCustomer table is created with columns Customer_Id and CustomerType_Id so this is fine.

I then create the customer like so:

// code behind
var customer = new Customer();
customer.name = txtCustomerName.Text;

using (var context = new MyEntities())
{   
    context.Customers.Add(customer);
    context.SaveChanges();  
}

I had a look here Insert/Update Many to Many Entity Framework . How do I do it? and tried to do something similar with customer types:

var customer = new Customer();
customer.name = txtCustomerName.Text;

// only starting with one customer type selected in a check box list
CustomerType customerType = context.CustomerTypes.FirstOrDefault(i => i.Id == 1);

using (var context = new MyEntities())
{
    // IncidentTypes throws Object reference not set to an instance of an object
    customer.CustomerTypes.add(customerType);

    context.Customers.Add(customer);
    context.SaveChanges();  
}

Am I missing something obvious here?

Thanks in advance.

Edit: for some reason I only have .Add( no AddToObject, AttachTo etc.

  • 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-06T21:02:57+00:00Added an answer on June 6, 2026 at 9:02 pm

    You must initialize CustomersTypes collection first.

    customer.CustomerTypes = new List<CustomerType>();
    

    You can also add this initialization to your Customer‘s constructor.

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

Sidebar

Related Questions

I have a heterogeneous List that can contain any arbitrary type of object. I
Suppose I have a customer object that has all of the standard customer properties
OK so I have a strongly-typed Customer Details view that takes a Customer object
I have a view for my customer object that allows him to choose a
I have webservice that can return data in multiple formats. For example json and
I need to have multiple databases for different customers. How can I handle multiple
I have a WCF service that can return several different collections. Objects in each
I'm using linq to pull back an object (i.e. customer) that might have a
I have a Customer object which has a collection of ContactNumbers. Is it possible
I am using hibernate, struts, extjs in my project. I have a Customer object

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.