How can I recognize EF POCO proxies without reference to EF?

I need to define EF POCO proxies; MSDN gives some hints based on ObjectContext.GetObjectType(type.GetType())

However, I would really like to do this without an EF link. For example, with NHibernate, I can check if the object implements the marker interface using the name (as a string) "NHibernate.Proxy.INHibernateProxy" .

Is there something similar in the EF POCO proxy? For example, can I rely on them being in the System.Data.Entity.DynamicProxies. namespace System.Data.Entity.DynamicProxies. , or is it fragile?

Taking a look inside the reflector, he simply checks the assembly for internally tracked assemblies, which is problematic for me.

+6
source share
2 answers

When checking under the hood, as an implementation detail, it is true that in the current EF the type will always be in "System.Data.Entity.DynamicProxies". This is probably not a reliable test, but it should change infrequently. However, I will try to clarify this with Microsoft.

+3
source

I know the POCO proxy type named like this template, UserDefinedName_123AF...

So what about this approach?

 const string pattern = @"_[\dA-F]{64}\b"; Regex regex = new Regex(pattern); bool result = regex.IsMatch(tragetObject.GetType().Name); 
+1
source

Source: https://habr.com/ru/post/892820/


All Articles