0

According to Why do navigation properties have to be public for a proxy to be created? a navigation property can be protected internal virtual and does not need to be public virtual and Entity Framework will still supply proxies. I have programmed a navigation property like this:

protected internal virtual ICollection<MyEntityType> MyNavigationCollection { get; set; }

In the mapping I obviously have:

.WithMany(t => t.MyNavigationCollection )

This seems to be in line with the article I referenced. The problem I have is, that Entity Framework no longer assigns an instance of a proxy collection to MyNavigationCollection when I query for the owning object, as I have changed the visibility to protected internal virtual for MyNavigationCollection .

What do I miss in order to have Entity Framework to use proxy objects for collections having the visitility protected internal virtual?

Community
  • 1
  • 1
Henrik Dahl
  • 121
  • 1
  • 1
  • 7

2 Answers2

0

The prop CANNOT be internal. It is not accessible to the Entity Framework code if it is marked internal. Remove the internal and it should work.

Proxies are not pre defined in your assembly, therefore internal won't work.

Internal items are only visible to the assembly that contains them, the EF code is external to your assembly so the proxy will not be able to access it, the proxy has full visibility to protected items because it is inheriting your class which makes the protected members visible in the proxy.

Paul Swetz
  • 2,234
  • 1
  • 11
  • 28
  • Are you sure? protected internal means both protected as well as internal, so why should broadening the visibility by internal make it less visible to Entity Framework? – Henrik Dahl Jul 14 '16 at 15:47
  • Internal items are only visible to the assembly that contains them, the EF code is external to your assembly so the proxy will not be able to access it, the proxy has full visibility to protected items because it is inheriting your class which makes the protected members visible in the proxy. – Paul Swetz Jul 14 '16 at 15:49
  • The comments in the SO you referenced explain this as well. internal simply will not work. Protected is fine. – Paul Swetz Jul 14 '16 at 15:52
  • Can somebody confirm, that you have indeed made a protected navigation property and it was being served by a proxy object? – Henrik Dahl Jul 14 '16 at 22:26
  • Just make two class library projects, reference one with the other. Then inherit a class from one in the other and you will see protected internals are not available to the inheriting class in the other library. – Paul Swetz Jul 15 '16 at 12:21
0

A bit late but this needs to be corrected because it is possible. As I'm always doing this because it is a best practise in DDD to hide your updatable collections. And exposing your collections gives a warning.

So I came in a situation that in one case it did worked as usual. But then an other navigation property did not work. Then I discovered the difference...

What does not work is this

HasRequired(x => x.Source).WithMany(t => t.MyNavigationCollection);

What you have to do is to configure it on the other end like this

HasMany(t => t.MyNavigationCollection).WithRequired(x => x.Source);

(Not sure if you need both though.)

Hopefully it is still helpful!

verbedr
  • 1,784
  • 1
  • 15
  • 18