0

I am mapping some pre-existing Business Objects to our database using Entity Framework. These object were originally using a home-grown data access method, but we wanted to try out Entity Framework on it now that it is using Code-First. It was my expectation that this would be fairly simple, but now I am having some doubts.

I am trying to use only attributes to accomplish this so that I don't have some of the mapping here, some of it there, and still more of it over there....

When I query for entities, I am getting System.Data.Entity.DynamicProxies.MyClass_23A498C7987EFFF2345908623DC45345 and similar objects back. These objects have the data from the associated record there as well as related objects (although those are DynamicProxies also).

What is happening here? Is something going wrong with my mapping? Why is it not bringing back MyBusinessObject.MyClass instead?

1 Answer 1

3

That has nothing to do with mapping. Those types you see are called dynamic proxies. EF at runtime derives class from every type you map and use it instead of your type. These classes has some additional internal logic inside overriden property setters and getters. The logic is needed for lazy loading and dynamic change tracking of attached entities.

This behaviour can be turned off in context instance:

context.Configuration.ProxyCreationEnabled = false;

Your navigation properties will not be loaded automatically once you do this and you will have to use eager loading (Include method in queries) or explicit loading.

Sign up to request clarification or add additional context in comments.

3 Comments

OK so this is expected behaviour having not turned it off. My next question is, how do I get something like WPF to recognize the object type? I have DataTemplates set up for MyClass, not a Dynamic Proxy. And I want to use lazy loading... I am starting to think that it may be best to ditch our original business classes and re-create them through the EF wizard and customize from there..
MyClass is parent of that dynamic class. Is it problem for WPF?
Double Take!!! Aparently not. I could have sworn that previously I had tried this and the DataTemplate had not been applied. Alright, well then disregard. Sometimes I think these computers have it in for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.