The problem here is that the code is not actually using proxies. This is because the entity instances are created using the New operator before being added to the context. The New operator always creates instances of the exact type that your specify and so there is no way for a proxy instance to be created. The code then saves changes to the context and runs a query on the same context. But this context already contains the two non-proxy instances which means no new instances are created and the entities remain non-proxies.
There are a few things you can do here:
- Use the DbSet.Create method to create your entity instances instead of using New. Using Create allows EF to create and return a proxy instance.
- Don't reuse the same context for the query. Obviously the code above is not real application code, so it's hard to know what your architecture is, but often good architectures make use of short-lived contexts such that you will create a context for adding and saving the entities and then in another place you will create a context for querying and updating.
- Consider not using change tracking proxies at all, since usually snapshot change tracking is a better approach. See http://blog.oneunicorn.com/2011/12/05/should-you-use-entity-framework-change-tracking-proxies/ for details.
Thanks,
Arthur