Question

Dynamic proxies have names like ClassName_00394CF1F92740F13E3EDBE858B6D599DFAF87AA5A089245977F61A32C75AA22 where the POCO class was simply called Classname.

I know to can get the POCO type of an EF dynamic proxy instance with ObjectContext.GetObjectType(instance.GetType()), but is there an easier way to get the proxy type of a given EF class than this:

databaseContext.TableName.First().GetType();

As this requires there be an instance of the type in the table (usually the case, but this smells "wrong").

I am passing class types to a function (like below) so that certain parent/child drag & drop rules can be applied to TreeNodes (the object instances are referenced by Tag property of TreeNodes so have the dynamic type for any EF objects).

if (!AllowChildDrop(e.Node, e.TargetNode, e.DropPosition, typeof(Answer)), typeof(Question)))

So basically is there an easier way to get the dynamic type than this?

if (!AllowChildDrop(e.Node, e.TargetNode, e.DropPosition, context.Answer.First().GetType(), context.Question.First().GetType()))
Was it helpful?

Solution

There is currently no way to avoid creating an instance to discover the runtime Type.

You should use DbSet.Create() not First()

  1. to avoid a potential trip to the database
  2. the First() instance may not be a proxy (e.g. you added an entity earlier in the call chain)

see here for more information

[the] instance is not added or attached to the set. The instance returned will be a proxy if the underlying context is configured to create proxies and the entity type meets the requirements for creating a proxy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top