EF4 Cast DynamicProxies to underlying object

后端 未结 5 2208
青春惊慌失措
青春惊慌失措 2020-12-15 06:55

I\'m using Entity Framework 4 with POCO template.

I have a List where MyObject are dynamic proxies. I want to use the XmlSerializer to serialize this list, but I don

5条回答
  •  长情又很酷
    2020-12-15 07:21

    As you don't want to turn ProxyCreation off, you are stuck DynamicProxy objects wherever you put virtual keyword for object property (EF Context inherits your object and replaces virtual properties with DynamicProxy objects). These DynamicProxy objects do not inherit from your POCO entities, they just have same properties and can be used instead of your POCO. If you really must to convert to POCO object (and I don't believe that someone will come up with a way to cast it), you may try to workaround by writing copy constructor which will copy all properties from passed argument (not very smart from performance standpoint, but what you have to do, you have to do), or maybe using System.Xml.Serialization.XmlTypeAttribute in parent object which contains your dynamic proxy instead of poco to tell serializer how to serialize virtual property (into which type).

提交回复
热议问题