Project Silk: A litle bit confused about ICollection?

Aug 21, 2011 at 10:27 AM
Edited Aug 21, 2011 at 12:08 PM


Thanks for the great web guidance. I confused about below. Event if i know how to avoid the usages. What does that means?



Note that the IReminderRepository interface returns collections as IEnumerable<T>, rather than IList<T> or ICollection<T>. This was an intentional design choice to prevent the addition of entities to the collections directly. To create a new reminder, the developer must use the Create method.




 How to defind one-to one relationship? (Means DataModel)

public class User { public int UserId { get; set; }
    public string Name { get; set; }
    public int BillingAddressId { get; set; }
    public int DeliveryAddressId { get; set; }
    public Address BillingAddress { get; set; }
    public Address DeliveryAddress { get; set; } or public ICollection<Address> DeliveryAddress { get; set; }


public class Address
    public int AddressId { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }


Which is the right things to do?. I know how to override.protected override void OnModelCreating(DbModelBuilder modelBuilder).

 public Address DeliveryAddress { get; set; } or public ICollection<Address> DeliveryAddress { get; set; }

Just confused and didn't have time to try.


Thanks and Best Regards.

Aug 23, 2011 at 5:36 PM
Edited Nov 2, 2011 at 5:34 PM


For your first question, the use of IEnumerable<T> instead of IList<T> is intended to avoid giving the programmer the possiblity to add items to that collection directly, through the use of the IList<T>.Add method, for example. This is useful in order to guarantee that you won't experience desynchronization issues accidentally; for example, if you add entities to those collections, the entities will be present in them, but not in the actual repository, but if you are forced to use the repository's methods to add entities to that collection, then you can't possibly step into this problem.

As for your second question, you might find the following resources useful to get more familiar with how to define relationships when using the EntityFramework code-first approach:

Entity Framework Code First Tutorial Supplement: What is Going on in a Fluent API Call

Entity Framework Tutorials

I hope you find this helpful.

Guido Leandro Maliandi

Aug 24, 2011 at 7:20 AM

Hi GuidoMaliandi,

Thanks again everythings was clear.