✅ Why for some navigation properties, we use setters while for some we initialise a list
Hello guys, consider the following two models that will be used to create a member table and lending table using EF Core. My question is, why for the navigation propery, when a collection is used like
ICollection
, we don't need to use set
in the curly braces, while if it's a single object, like Books? Book
, we use get and set. What's the difference here please. We directly assign a new list when using collection for example.
6 Replies
because it's not necessary to assign a new collection to modify the items inside
that's not really an EF thing, it's just a "how you want to expose collections in a class" thing
hmmm yeah, each time we use the same collection that we initialized when creating a lending object; we can add to the list or remove to it without needing another reference? But hmm I'm still confuse, why for books we don't do that? :c
Because it's not a list
Or any other sort of collection
yep I see
last question, notice the collection is not nullable, like we didn't use the
?
together with the collection, is there a use-case where we would want a list to be nullable?
well no I think it's counter-intuitive... we are assigning a new list, the idea is we do that so that we don't have to check for null reference; we don't want to have a NullPointerException?Yeah, the list is instantiated in every case, it will never be
null
Yep, thanks !