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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:39:38+00:00 2026-05-26T10:39:38+00:00

For each entity that has a one to many relation with other entity when

  • 0

For each entity that has a one to many relation with other entity when i trying to add a new item it seems like i have to define these list of items that relates to this entity.
For example, lets say that i have a ProductType entity that has a list of Products as following:

[Table]
public class ProductType
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true)]
    public int Id { get; private set; }
    [Column]
    public string Name { get; set; }

    private EntitySet<Product> _products;
    [Association(Storage = "_products", ThisKey = "Id", OtherKey = "ProductTypeId")]
    public EntitySet<Product> Products
    {
        get { return _products; }
        set { _products.Assign(value); }
    }
}

when i try to add a new ProductType like that:

ProductType newType = new ProductType { Name = "newType" };
_productRepository.Add(newType);     //InsertOnSubmit() and SaveChanges()

it gives me an exception that Products is null:

Object reference not set to an instance of an object

the same problem with all the entities that has a one to many relation with other entities. so how can i allow null for the EntitySet<> that represents a one to many Relation

  • 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-05-26T10:39:38+00:00Added an answer on May 26, 2026 at 10:39 am

    You’re missing the generated constructor setting the default for the Products entity set. Compare this against what the designer generates for you. For example, the Product constructor for Northwind looks like this:

        public Product()
        {
            this._Order_Details = new EntitySet<Order_Detail>(new Action<Order_Detail>(this.attach_Order_Details), new Action<Order_Detail>(this.detach_Order_Details));
            this._Category = default(EntityRef<Category>);
            this._Supplier = default(EntityRef<Supplier>);
            OnCreated();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Column(Fields) that contain multiple values for each entity (One to Many).
I have many to one relation, let's say User has a Company. Company entity
Each of my clients can have many todo items and every todo item has
I have an entity set of NotificationTemplates , and each one of these has
I have a Department entity which has a one-to-many relationship to an Employee entity.
Basically each entity would have a collection of feeding behaviors that it would follow.
Each item in a WPF ListBox control seems to have a bit of left
I have a User entity that has a Current Location field (city and country).
How would one structure a table for an entity that can have a one
I'm using Entity Framework 4.1 and trying to create a one-to-many relationship. In my

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.