C
C#7d ago
Faker

✅ 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.
C#
namespace Week5.Models;

public class Member
{
public int MemberId { get; set; }
public string FirstName { get; set; } = null!;
public string LastName { get; set; } = null!;

// Navigation property - One member can have many lendings
public ICollection<Lending> Lendings { get; } = new List<Lending>();
}
C#
namespace Week5.Models;

public class Member
{
public int MemberId { get; set; }
public string FirstName { get; set; } = null!;
public string LastName { get; set; } = null!;

// Navigation property - One member can have many lendings
public ICollection<Lending> Lendings { get; } = new List<Lending>();
}
C#
namespace Week5.Models;

public class Lending
{
public int LendingId { get; set; }
public DateTime IssueDate { get; set; }
public DateTime DueDate { get; set; }
public DateTime ReturnDate { get; set; }

// Foreign keys
public int MemberId { get; set; }
public int BookId { get; set; }

// Navigation property - one lending can have one member; one lending can have one book;
public Books? Book { get; set; }
public Member? Member { get; set; }

}
C#
namespace Week5.Models;

public class Lending
{
public int LendingId { get; set; }
public DateTime IssueDate { get; set; }
public DateTime DueDate { get; set; }
public DateTime ReturnDate { get; set; }

// Foreign keys
public int MemberId { get; set; }
public int BookId { get; set; }

// Navigation property - one lending can have one member; one lending can have one book;
public Books? Book { get; set; }
public Member? Member { get; set; }

}
6 Replies
Jimmacle
Jimmacle7d ago
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
Faker
FakerOP7d ago
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
Angius
Angius7d ago
Because it's not a list Or any other sort of collection
Faker
FakerOP7d ago
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?
Angius
Angius7d ago
Yeah, the list is instantiated in every case, it will never be null
Faker
FakerOP7d ago
Yep, thanks !

Did you find this page helpful?