zoukankan      html  css  js  c++  java
  • EV: Working with Proxies

    Keywords: Entity Framework, EF, Proxy, Dynamic class, Entity Type

     

    When creating instances of POCO entity types, the Entity Framework often creates instances of a dynamically generated derived type that acts as a proxy for the entity. This proxy overrides some virtual properties of the entity to insert hooks for performing actions automatically when the property is accessed. For example, this mechanism is used to support lazy loading of relationships. The techniques shown in this topic apply equally to models created with Code First and the EF Designer.

    Disabling proxy creation

    Sometimes it is useful to prevent the Entity Framework from creating proxy instances. For example, serializing non-proxy instances is considerably easier than serializing proxy instances. Proxy creation can be turned off by clearing the ProxyCreationEnabled flag. One place you could do this is in the constructor of your context. For example:

    
    
    1. public class BloggingContext : DbContext
    2. {
    3.     public BloggingContext()
    4.     {
    5.         this.Configuration.ProxyCreationEnabled = false;
    6.     } 
    7.  
    8.     public DbSet Blogs { get; set; }
    9.     public DbSet Posts { get; set; }
    10. }

    Note that the EF will not create proxies for types where there is nothing for the proxy to do. This means that you can also avoid proxies by having types that are sealed and/or have no virtual properties.

    Explicitly creating an instance of a proxy

    A proxy instance will not be created if you create an instance of an entity using the new operator. This may not be a problem, but if you need to create a proxy instance (for example, so that lazy loading or proxy change tracking will work) then you can do so using the Create method of DbSet. For example:

    
    
    1. using (var context = new BloggingContext())
    2. {
    3.     var blog = context.Blogs.Create();
    4. }

    The generic version of Create can be used if you want to create an instance of a derived entity type. For example:

    
    
    1. using (var context = new BloggingContext())
    2. {
    3.     var admin = context.Users.Create();
    4. }

    Note that the Create method does not add or attach the created entity to the context.

    Note that the Create method will just create an instance of the entity type itself if creating a proxy type for the entity would have no value because it would not do anything. For example, if the entity type is sealed and/or has no virtual properties then Create will just create an instance of the entity type.

    Getting the actual entity type from a proxy type

    Proxy types have names that look something like this:

    System.Data.Entity.DynamicProxies
    .Blog_5E43C6C196972BF0754973E48C9C941092D86818CD94005E9A759B70BF6E48E6

    You can find the entity type for this proxy type using the GetObjectType method from ObjectContext. For example:

    
    
    1. using (var context = new BloggingContext())
    2. {
    3.     var blog = context.Blogs.Find(1);
    4.     var entityType = ObjectContext.GetObjectType(blog.GetType());
    5. }

    Note that if the type passed to GetObjectType is an instance of an entity type that is not a proxy type then the type of entity is still returned. This means you can always use this method to get the actual entity type without any other checking to see if the type is a proxy type or not.

  • 相关阅读:
    编译gcc报错make[3]: Leaving directory `/usr/local/src/gcc-7.4.0/build/gcc' make[2]: *** [all-stage1-gcc] Error 2 处理
    ERROR 1176 (42000): Key 'XXX' doesn't exist in table 'XXX'报错处理
    /lib64/libc.so.6: version `GLIBC_2.18' not found报错解决
    Centos7上pkg-config的安装
    ERROR: Error in Log_event::read_log_event(): 'Found invalid event in binary log', data_len: 31, event_type: 35报错处理
    MySQL5.7主从复制slave报Last_Errno: 1146错误解决
    详述 hosts 文件的作用及修改 hosts 文件的方法
    Java Decompiler(Java反编译工具)
    使用Charles代理工具导致电脑无法正常访问网站(您的连接不是私密连接)
    阿里云服务器Svn-Server无法连接,阿里云服务器SVNServer配置
  • 原文地址:https://www.cnblogs.com/weihongji/p/3493262.html
Copyright © 2011-2022 走看看