Giter Site home page Giter Site logo

Comments (4)

markjprice avatar markjprice commented on August 24, 2024 1

Products should not be null because you should have set Products to an empty HastSet<T> in the Category constructor, as shown on page 450:
image

from cs11dotnet7.

markjprice avatar markjprice commented on August 24, 2024 1

When you say "I initially didn't include the Category()", if you are referring to the following code:

public Category()
{
  Products = new HashSet<Product>();
}

Then that is a constructor and they are used all the time. The book covers constructors in Chapter 5. You might want to re-read it to remind yourself why they are used in almost all classes.

The code does use a navigation property to establish a relationship between entities. That is what Products is.

But if you do not use the Include extension method to load related data, then the Products navigation property would be null and would throw an exception when you try to read its Count, so you must set it to an empty collection when an instance of Category is instantiated, which is exactly what the code you didn't add does.

from cs11dotnet7.

cmkaya avatar cmkaya commented on August 24, 2024

Hello Mark,

Thank you for your response. To be honest, I initially didn't include the Category() because I didn't fully grasp the concept. However, it's much clearer to me now.

I'd like to ask whether this approach is commonly employed in real-world applications. The reason I'm raising this question is because this is the first instance I've encountered this approach. Typically, I've found that navigation properties suffice for establishing relationships between entities.

from cs11dotnet7.

cmkaya avatar cmkaya commented on August 24, 2024

Thank you for taking the time to address my issue.

from cs11dotnet7.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.