C# DOT.NET System.Data.Entity.DbContext,将父对象映射到子对象,但不将子对象映射到父对象
我使用的是npgsql.entityframework,由于XmlSerializer实现的某些方面,我希望对映射进行自定义处理 我希望使用parent_id(尽可能长)映射子对象,而不是将子对象映射到父对象(作为父对象)的标准方法 我希望保留在模型中向父对象添加子对象的功能,但我希望能够在不加载父对象的情况下处理子对象 vb.net中的代码(答案是c#或vb) 示例父添加: dim p作为新父级 p、 添加(新子级) 未加载父项但父项id已知的子项添加示例, dim c作为新生婴儿 c、 父级\u id=已知的\u父级\u id 非常感谢您的帮助 公共类父类C# DOT.NET System.Data.Entity.DbContext,将父对象映射到子对象,但不将子对象映射到父对象,c#,vb.net,entity-framework,entity-framework-6,C#,Vb.net,Entity Framework,Entity Framework 6,我使用的是npgsql.entityframework,由于XmlSerializer实现的某些方面,我希望对映射进行自定义处理 我希望使用parent_id(尽可能长)映射子对象,而不是将子对象映射到父对象(作为父对象)的标准方法 我希望保留在模型中向父对象添加子对象的功能,但我希望能够在不加载父对象的情况下处理子对象 vb.net中的代码(答案是c#或vb) 示例父添加: dim p作为新父级 p、 添加(新子级) 未加载父项但父项id已知的子项添加示例, dim c作为新生婴儿 c、 父级
Private m_childs As ICollection(Of Child)
<Runtime.Serialization.DataMember(), ComponentModel.DataAnnotations.Key>
Public Property id As Long
Public Overridable Property childs() As ICollection(Of child)
Get
Return m_childs
End Get
Set(value As ICollection(Of child))
m_childs = value
End Set
End Property
私有m_childs作为i集合(子集合)
公共财产id,只要
公共可重写属性childs()作为(子对象的)ICollection
得到
返回m_childs
结束
设置(值为ICollection(子对象))
m_childs=值
端集
端属性
末级
公营儿童
<Runtime.Serialization.DataMember(), ComponentModel.DataAnnotations.Key>
Public Property id As Long
Public Property parent As parent ' works but not desired
Public Property parent_id As Long' want to use this
公共财产id,只要
作为父对象的公共属性父对象可行,但不理想
公共属性父项\u id,只要“要使用此”
末级
/johan找到了一个解决方案,在子映射到父映射中使用ForeignKey,这两种方法都可以使用
<System.ComponentModel.DataAnnotations.Schema.ForeignKey("parent")> _
Public Property parent_id As Long
<Xml.Serialization.XmlIgnore> _
Public Property parent As parent
_
公共财产父项\u id的长度
_
作为父母的公共财产