当对象添加到父集合的子集合时,未在子节点上设置父关系 [英] Parent relationship not set on child, when object added to Children collection of parent

查看:50
本文介绍了当对象添加到父集合的子集合时,未在子节点上设置父关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 


 

      public     class    DocumentLink  
     {
         public   virtual   int   DocumentLinkID
         ; {
             get ;

             set ;
         }
         public   虚拟  int   DocumentID
  &nbs p;      {
             get ;
             set ;        }

         [ ForeignKey " DocumentID" )]       ;   public   virtual   文献 文献
         {
        &NBSP ;   的获得;
           &NBSP ; 设置;
      &n bsp; }    }

 


 


 

      public    class   文档  
     {
         public   virtual   int   DocumentID
         {
    ;         的获得;
     &NBSP ;       set ;
        }

         private   ICollection的< <温泉n style ="color:#2b91af"> DocumentLink
>  _DocumentLinks;
         [ ForeignKey " DocumentID" )]         public   virtual   ICollection < DocumentLink >  DocumentLinks         {
  &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;   if  (_ DocumentLinks  ==  null
   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;& NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; _DocumentLinks&NBSP; =&NBSP;的&NBSP;的 ObservableCollection < DocumentLink >();

      ;            return   _DocumentLinks;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;  set
             {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; _DocumentLinks&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 ;    文档  d  =  new   文档();  //  context.Documents.FirstOrDefault();
  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的 DocumentLink   dl  =  new   DocumentLink ();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; d.DocumentLinks.Add(DL);

  dl.Document返回null,甚至dl.DocumentID = 0


 


 


NeoTech

解决方案


这里有几个问题:




  • 你应该删除[ForeignKey]属性关闭Document.DocumentLinks(Code First创建两个单独的关系,而不是一个具有反向导航属性的关系)

    实际上你也可以实际删除其他属性,如果你想,Code First将按惯例拿起外键
  • 在你发布的代码中EF不在图片中,所以代码只是使用你写的类定义

    我请注意,您已将所有属性设置为虚拟,因此您实际上可以获得EF为您创建代理,以便完成您想要的修复:


文件d = ctx.Documents.Create(); 
DocumentLink dl = ctx.DocumentLinks.Create();
d.DocumentLinks.Add(dl);


 

 

    public  class DocumentLink
    {
        public virtual int DocumentLinkID
        {
            get;
            set;
        }

        public virtual int DocumentID
        {
            get;
            set;
        }

        [ForeignKey("DocumentID")]
        public virtual Document Document
        {
            get;
            set;
        }
    }

 

 

 

    public class Document 
    {
        public virtual int DocumentID
        {
            get;
            set;
        }

        private ICollection<DocumentLink> _DocumentLinks;
        [ForeignKey("DocumentID")]
        public virtual ICollection<DocumentLink> DocumentLinks
        {
            get
            {
                if (_DocumentLinks == null)
                    _DocumentLinks = new ObservableCollection<DocumentLink>();

                return _DocumentLinks;

            }
            set
            {
                _DocumentLinks = value;
            }
        }
    }

 

If you execute the below code

 

                    Document d = new Document(); // context.Documents.FirstOrDefault();
                    DocumentLink dl = new DocumentLink();
                    d.DocumentLinks.Add(dl);

 dl.Document returns null, even dl.DocumentID = 0

 

 


NeoTech

解决方案

Hi,

There are a couple of issues here:

  • You should remove the [ForeignKey] attribute off Document.DocumentLinks (Code First is creating two separate relationships rather than one with inverse navigation properties)
    In fact you can actually remove the other attribute too, if you want, Code First will pick up the foreign keys by convention
  • In the code you posted EF isn't in the picture so the code is just using the class definitions you wrote
    I notice you made all you properties virtual so you can actually get EF to create a proxy for you that will take care of doing the fix up you want:

Document d = ctx.Documents.Create();
DocumentLink dl = ctx.DocumentLinks.Create();
d.DocumentLinks.Add(dl);


这篇关于当对象添加到父集合的子集合时,未在子节点上设置父关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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