Comments (4)
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:
from cs11dotnet7.
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.
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.
Thank you for taking the time to address my issue.
from cs11dotnet7.
Related Issues (20)
- Packt.Shared library not used for using Product type HOT 1
- Chapter 12: Creating a class library for entity models using SQLite' help needed. HOT 4
- Page 244 init-only properties HOT 1
- .Net standard is legacy but is still used? HOT 3
- Fix to the newer versions of Microsoft.Configuration.Extensions HOT 1
- Exercise 15.2 HOT 6
- No OmniSharp: Select Project HOT 5
- "Value" setting is also needed in appsettings.json, Chapter 04 switch trace levels HOT 5
- Exception when logging source code - chapter 4 HOT 1
- No instructions on how to create a unit test app HOT 5
- How GAC lead to issue if an app needs a specific version ? HOT 2
- Pattern Matching with Regular Expressions HOT 1
- Addin `Model` into the Razor page cause error HOT 5
- Page 59 6th paragraph
- Page 149 HOT 2
- Exercise Solutions - Where are they? HOT 1
- Relational pattern + property pattern example missing decimal literal value suffix "M" HOT 1
- Sentence out of place in chapter 7 HOT 1
- Scaffolding
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cs11dotnet7.