CTP4 Code First,在将新对象添加到上下文后,Lazy Loading无法正常工作 [英] CTP4 Code First, Lazy Loading not working after adding new object to context

查看:58
本文介绍了CTP4 Code First,在将新对象添加到上下文后,Lazy Loading无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将新对象添加到ObjectSet然后在ObjectContext上调用SaveChanges(启用了延迟加载)后,似乎新对象上的延迟加载无效。


例如,如果我有两个类,如下所示,则首先使用代码进行配置,如下所示:


   公共课MyClass1

    {

        public int MyClass1ID {get;组; }


        public string SomeProperty {get;组; }


        public int SomeOtherProperty {get;组; }


        public int MyClass2ID {get;组; }


        public virtual MyClass2 MyClass2 {get;组; }


    }


   公共课MyClass2

    {

        public int MyClass2ID {get;组; }


        public int PropertyX {get;组; }


        public int PropertyY {get;组; }
    }


   公共类MyClass1Configuration:EntityConfiguration< MyClass1>

    {

        public MyClass1Configuration()

        {

            HasKey(mc1 => mc1.MyClass1ID);


           属性(MC1 => mc1.MyClass1ID).IsIdentity();


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; HasRequired(MC1 => mc1.MyClass2).HasConstraint((MC1,MC2)=> mc1.MyClass2ID == mc2.MyClass2ID);


&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; MapSingleType(MC1 =>新建

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;                   {

   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&N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NBSP;&NBSP;&NBSP;&NBSP; }
                ).ToTable(" T_MYCLASS1_TABLE");

        }
    }


   公共类MyClass2Configuration:EntityConfiguration< MyClass2>

    {

        public MyClass2Configuration()

        {

            HasKey(mc2 => mc2.MyClass2ID);


           属性(MC2 => mc2.MyClass2ID).IsIdentity();


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; MapSingleType(MC2 =>新建

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;                   {

   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; mc2.MyClass2ID,

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; mc2.PropertyX,

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N BSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; mc2.PropertyY

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
                ).ToTable(" T_MYCLASS2_TABLE");
$
        }
    }


如果创建了新的MyClass1,并且其MyClass2ID设置为数据库中现有MyClass2条目的ID。 然后调用AddObject将新的MyClass1对象添加到其ObjectSet,然后再添加ObjectContext.SaveChanges()。 然后尝试和
访问刚添加的新MyClass1对象上的MyClass2属性,它是null并且不会从数据库中加载延迟。 我原以为它会延迟加载属性MyClass2对象,因为MyClass1的MyClass2ID被设置为数据库中的有效ID条目
。 如果我从数据库中查询现有的MyClass1然后尝试访问其MyClass2属性,则延迟加载工作正常。


 

解决方案


 


这是因为延迟加载是通过创建一个来实现的在运行时从
  派生对象的代理(以便EF可以在属性中注入一些额外的逻辑来执行延迟加载)。因此,不是只调用对象的默认构造函数,而是调用ObjectSet.CreateObject()来获取
a作为代理的新实例,然后延迟加载也将对新创建的对象起作用。


 


希望这有帮助。


 


~Rowan


After adding a new object to an ObjectSet and then calling SaveChanges on the ObjectContext (with Lazy Loading enabled), it seems that lazy loading on the new object is not working.

For example, if I have two classes like below, that are configured using code first like the following:

    public class MyClass1
    {
        public int MyClass1ID { get; set; }

        public string SomeProperty { get; set; }

        public int SomeOtherProperty { get; set; }

        public int MyClass2ID { get; set; }

        public virtual MyClass2 MyClass2 { get; set; }

    }

    public class MyClass2
    {
        public int MyClass2ID { get; set; }

        public int PropertyX { get; set; }

        public int PropertyY { get; set; }
    }

    public class MyClass1Configuration : EntityConfiguration<MyClass1>
    {
        public MyClass1Configuration()
        {
            HasKey(mc1 => mc1.MyClass1ID);

            Property(mc1 => mc1.MyClass1ID).IsIdentity();

            HasRequired(mc1 => mc1.MyClass2).HasConstraint((mc1, mc2) => mc1.MyClass2ID == mc2.MyClass2ID);

            MapSingleType(mc1 => new
                                     {
                                         mc1.MyClass1ID,
                                         mc1.MyClass2ID,
                                         mc1.SomeProperty,
                                         mc1.SomeOtherProperty
                                     }
                ).ToTable("T_MYCLASS1_TABLE");
        }
    }

    public class MyClass2Configuration : EntityConfiguration<MyClass2>
    {
        public MyClass2Configuration()
        {
            HasKey(mc2 => mc2.MyClass2ID);

            Property(mc2 => mc2.MyClass2ID).IsIdentity();

            MapSingleType(mc2 => new
                                     {
                                         mc2.MyClass2ID,
                                         mc2.PropertyX,
                                         mc2.PropertyY
                                     }
                ).ToTable("T_MYCLASS2_TABLE");
        }
    }

If a new MyClass1 is created and its MyClass2ID is set to an ID of an existing MyClass2 entry that is in the database.  Then AddObject is called to add the new MyClass1 object to its ObjectSet and then ObjectContext.SaveChanges().  Then try and access the MyClass2 property on the new MyClass1 object just added and it is null and does not get lazy loaded from the database.  I would have thought it would lazy load the property MyClass2 object since MyClass1's MyClass2ID is set to a valid ID entry in the database.  Lazy loading works fine if I query an existing MyClass1 from the database and then try and access its MyClass2 property.

 

解决方案

Hi,

 

This is because lazy loading is achieved by creating a proxy that derives from  your objects at runtime (so that EF can inject some additional logic into the properties to perform the lazy loading). So rather than just calling the default constructor on your object you can call ObjectSet.CreateObject() to get a new instance that is a proxy, then lazy loading will work on your newly created objects too.

 

Hope this helps.

 

~Rowan


这篇关于CTP4 Code First,在将新对象添加到上下文后,Lazy Loading无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆