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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:36:09+00:00 2026-05-25T14:36:09+00:00

imagine having a simple POCO for EF 4.1 Codefirst: public class Product { //

  • 0

imagine having a simple POCO for EF 4.1 Codefirst:

public class Product
{
    // Native properties 
    [Key]
    public Guid ID { get; set; }

    // Navigation properties
    public virtual Category Category { get; set; }
    public virtual ICollection<Customer> Customers { get; set; }

    public Product()
    {
        this.ID = Guid.NewGuid();

        // Do I have to instantiate navigation properties in the constructor or not???
        this.Category = new Category();
        this.Customers = new List<Customer>();
    }
}

What I couldn’t figure out so far is if I should instantiate complex navigation properties in the POCO’s constructor or not?

Seems like all my current code is working if I don’t instantiate, but I’m concerned that my code might cause problems in the future.

Are there any rules, best practices or any side effects?

Thanks for your ideas and tips!

  • 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-25T14:36:10+00:00Added an answer on May 25, 2026 at 2:36 pm

    You don’t need to instantiate Category. Category is single entity which either exists or not – Product is not responsible for its creation. You can need to instantiate Customers to empty list.

    The reason why it works now is because your context will wrap entities with dynamic proxy which will handle instantiation of you Customers collection. Because of that other code can access the collection without receiving NullReferenceException. This can change if you create instance of Product in your code without using EF. In such case there will be no dynamic proxy and your collection will be null = you will have to instantiate it yourselves.

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

Sidebar

Related Questions

I am having trouble trying to get iterators for inner sub-containers. Basically imagine this
Let's imagine that we are having cars table with such a simple structure... car_id
I imagine this is fairly simple and one I should be able to get
Imagine having the following scenario: You need to create a system where the Back
Let's say I have a simple class Cat in C#, with a Name property
I'm experimenting with a simple HTML5 local storage based app and I'm having trouble
I am creating a simple game database and I am having some problems. There
I am having a lot of trouble deploying a simple rails3.1 app. It seems
This is so simple I can't believe I'm having so much trouble with it.
To clarify the question, imagine having a paginated list with clients. When clicked on

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.