【问题标题】:Is there any relation between dynamic proxies and lazy loading动态代理和延迟加载之间有什么关系吗
【发布时间】:2015-05-24 03:30:37
【问题描述】:

我对 EF 中的动态代理意味着什么感到困惑?动态代理与延迟或急切加载之间是否存在关系。

例如我有以下视图模型:-

public class ServerJoin
{
        public Server Server { get; set; }
        public Resource Resource { get; set; }
        public Technology Technology { get; set; }
        //public ComponentDefinition ComponentDefinition { get; set; }
        //public SystemInfo SystemInfo { get; set; }
        public SDOrganization Site { get; set; }
        public SDOrganization Customer { get; set; }
        public NetworkInfo NetworkInfo { get; set; }
        public ICollection<NetworkInfo> NetworkInfo2 { get; set; }
       [Display(Name="MAC Unique")]
        public bool IsMACUnique { get; set; }
        [Display(Name = "IP Unique")]
        public bool IsIPUnique { get; set; }
        public Nullable<double> SPEED { get; set; }
        public Nullable<Int64> PROCESSORCOUNT { get; set; }
        //public ResourceState AssetState { get; set; }
        public TechnologyIP TechnologyIP2 { get; set; }// this is use for inserting the Network info for the VM.
        [Display(Name = "IP Unique")]
        public bool IsTMSIPUnique { get; set; }
        [Display(Name = "MAC Unique")]
        public bool IsTMSMACUnique { get; set; }
}

目前在 Visual Studio 中,我在创建视图模型的新实例时发现了以下内容,其中一些值的类型为“count=12”,而其他值的类型为“动态代理”等:-

那么什么是动态代理,为什么有些值的类型是动态代理?

【问题讨论】:

    标签: asp.net-mvc entity-framework asp.net-mvc-5 entity-framework-5


    【解决方案1】:

    是的,Entity Framework 会在可能的情况下创建动态代理(通常是虚拟属性)。这些代理包装了类的实际实现,并允许您按照您的想法进行延迟加载。

    不同的 IoC 容器也使用相同的技术来提供像拦截器这样的 AOP 风格。

    【讨论】:

    • 但现在在我的服务器内部有一个 VirtualMachines 类型的集合,现在无论我是否指定,我都会将 VirtualMachine 类型设为“count=1”或“count=0”等。Include(a =>a.VirtualMachine) 与否?我的意思是不管我是否延迟加载 visrtualMachine 集合?
    • VirtualMachine 集合是如何定义的?是虚拟的吗?我认为如果您的属性是虚拟的,EF 只能创建动态代理。
    • 它的定义如下:- public virtual ICollection TMSVirtualMachines { get;放; }
    • 嗯,您是否考虑过仅使用调试器就可以使 EF 加载值?最后,当 Visual Studio 构建监视窗口时,它需要访问属性。确定它们是否被延迟加载的最佳方法是放置一个 SQL Server Profiler,看看到底发生了什么。
    • 现在在 sql profiler 上,当我获得信息时,将不会从数据库中检索 VirtualAMchine,因为我没有添加到 .Include(a=>a.VirtualMachine),但 VS 会显示正确的虚拟机数量!!!!不知道这是怎么发生的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多