0

I remember the last version of EF wasn't supporting protected or private collection mapping like NHibernate does.

You had to do something like that

public class Post
{
    ...

    public virtual ICollection<Tag> Tags { get; private set; }

Is there a way to avoid someone from calling this collection directly ? i would prefer encapsulating it in a method instead to have full control on it. However this was needed by EF in order to do the relationship. Was this changed ?

EDIT: Normally we should be able to use IEnumerable instead of ICollection (as it is supported with NHibernate) but it seem not supported in EF.

Thanks.

Rushino
  • 9,415
  • 16
  • 53
  • 93

1 Answers1

0

What you show in your example is mapping of private or protected collection, isn't it? - your setter is private and it is supported when using EDMX file for mapping.

In case of code-first it is not supported because both fluent API and conventions are able to map only properties which are visible to them.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 2
    Not at all. You can't set it that is true.. but you can get it.. which is bad in DDD approach. Normally you should manipulate your collection with methods such maybe i want someone to Add items.. with AddItem() but not remove any or whatever. Also, NHibernate enable us to make those properties private so why EF can't do this ? – Rushino Sep 14 '11 at 13:25
  • You can configure getter to be non public as well and that will make whole property non public. – Ladislav Mrnka Sep 14 '11 at 15:14