Why do navigation properties have to be public for

2019-07-02 01:01发布

问题:

At http://msdn.microsoft.com/en-us/library/dd468057.aspx I read that all navigation properties for which I would like to have a change tracking proxy need to be public and virtual. From what I understand, the CLR creates subclasses of my POCOs dynamically, and it re-implements the properties to provide the requested behaviour.

For this I undertand that the property needs to be virtual, and that it should have protected or higher accesability. However, if I want to use these for convenience within the assemby, but don't want to expose them, I prefer them not to be public. Which leads me to two questions.

  1. (for my understanding of what is going on) why does the runtime require the properties to be public rather than protected or internal?

  2. (for my actual situation) are there any workarounds to hide the navigation property, but still have the change tracking behaviour?

回答1:

Properties must be public (and virtual) OR protected (and virtual) for proxies to work.

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

Private won't work for obvious reasons (proxies inherit from your classes).