Why is my Entity Framework Code First proxy collection null and why can't I set it?

后端 未结 3 864
误落风尘
误落风尘 2020-12-01 01:38

I am using DBContext and have two classes whose properties are all virtual. I can see in the debugger that I am getting a proxy object when I query the context. However, a c

3条回答
  •  盖世英雄少女心
    2020-12-01 01:52

    Old question...

    Poco class:

    public partial class MyPOCO
    {
        public MyPOCO()
        {
            this.MyPocoSub = new HashSet();
        }
    
        //VIRTUAL
        public virtual ICollection MyPocoSub { get; set; }
    }
    

    and proxy code:

        public override ICollection MyPocoSubSets
        {
            get
            {
                ICollection myPocoSubSets = base.MyPocoSubSets;
                if (!this.ef_proxy_interceptorForMyPocoSubSets(this, myPocoSubSets))
                {
                    return base.MyPocoSubSets;
                }
                return myPocoSubSets;
            }
            set
            {
                if (value != this.RelationshipManager.GetRelatedEnd("WindowsFormsApplication.Models.MyPocoSubSet_MyPOCO", "MyPocoSubSet_MyPOCO_Source"))
                {
                    // EXCEPTION 
                    throw new InvalidOperationException("The property 'MyPocoSubSets' on type 'MyPOCO_A78FCE6C6A890855C68B368B750864E3136B589F9023C7B1D90BF7C83FD291AC' cannot be set because the collection is already set to an EntityCollection.");
                }
                base.MyPocoSubSets = value;
            }
        }
    

    As you can see that exception raised in proxy class in ExtityFramework 5. This means that behavior still exist.

提交回复
热议问题