Reputation: 121
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?
Upvotes: 0
Views: 460
Reputation: 2270
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!
Upvotes: 0
Reputation: 2254
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.
Upvotes: 0