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

  • Home
  • SEARCH
  • 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 8485135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:33:28+00:00 2026-06-10T20:33:28+00:00

I am new to entityframework, and was trying to insert data into relational tables.

  • 0

I am new to entityframework, and was trying to insert data into relational tables. Below is the description of my code and what error occured.

I have two tables,

  • Product

  • ProductInformation

I have designed the entities for both the tables like following:

Product-Entity:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<ProductInfomation> ProductInformations { get; set; }
}

ProductInformation-Entity:

public class ProductInfomation
{
    public int Id { get; set; }
    public string Description { get; set; }
    public string BatchNumber { get; set; }
    public DateTime ProductedOn { get; set; }

    public virtual Product Product { get; set; }
}

EntityContext Class:

public class EF : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<ProductInfomation> ProductInformations { get; set; }
}

To insert data into relational table, I`m following below procedure:

EF objEf = new EF();

        ProductInfomation pi = new ProductInfomation();
        pi.Description = "Best product in XYZ-Segment";
        pi.BatchNumber = "B001";
        pi.ProductedOn = DateTime.Now;

        Product prod = new Product();
        prod.Name = "Product-A";
        prod.ProductInformations.Add(pi); // This code throws exception.(Object reference not set to an instance of an object)

        objEf.Products.Add(prod);
        objEf.SaveChanges();

Database schema:

Product:
Id           INT        IDENTITY        PRIMARY KEY,
Name         VARCHAR(200)
------------------------------------------------------
ProductInformation:
Id           INT        IDENTITY        PRIMARY KEY,
Description  VARCHAR(500),
BatchNumber  VARCHAR(200),
ProductedOn  DATETIME,
Product_Id   INT        REFERENCES      Product(Id)
------------------------------------------------------

After running above code I get following error: Object reference not set to an instance of an object.

where am I going wrong?

  • 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-10T20:33:29+00:00Added an answer on June 10, 2026 at 8:33 pm

    This is null

    prod.ProductInformations
    

    when you are trying to add item to it. Please correct to:

    prod.Name = "Product-A";
    prod.ProductInformations = new Collection<ProductInfomation>();
    prod.ProductInformations.Add(pi); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to entity framework and patterns. How would I insert data into
How can I insert data into my database using Entity Framework ? I'm trying
I just declared some code-first models for a new project that uses EntityFramework. public
I'm trying to insert some data in my database using Entity Framework model, but
I am new to entity framework and I am trying to go the Code
I am using Entity Framework 4.3.1 and I am trying to insert a new
I'm trying to insert the contents of a DataTable verbatim into a SQL Server
I'm using a FormView control with the EntityFramework. I'm trying to add a new
I'm new to Entity Framework and am trying to figure things out. I have
I'm trying to update an object that I have previously saved with EntityFramework 4.1

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.