【问题标题】:DOT.NET System.Data.Entity.DbContext, mapping parent to child but not child to parentDOT.NET System.Data.Entity.DbContext,将父级映射到子级而不是子级到父级
【发布时间】:2015-08-14 15:40:43
【问题描述】:

我正在使用 npgsql.entityframework,由于 XmlSerializer 实现的某些方面,我想要自定义处理映射。

我希望孩子使用 parent_id(只要)映射,而不是标准方法将子映射到父(作为父)。

我想在模型中保留向父级添加子级的功能,但我希望能够在不加载父级的情况下处理子级。

vb.net 中的代码(答案是 c# 或 vb)

示例父添加: 暗淡 p 作为新的父母 p.child.add(新孩子)

示例子添加未加载父,但父 ID 已知, 昏暗的c作为新的孩子 c.parent_id=known_parent_id

非常感谢任何帮助。

公共班级家长

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

结束类

公共班级孩子

<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

结束类

/约翰

【问题讨论】:

    标签: c# vb.net entity-framework entity-framework-6


    【解决方案1】:

    找到了一个解决方案,在子映射中使用 ForeignKey 到父启用了这两种方法的使用。

    <System.ComponentModel.DataAnnotations.Schema.ForeignKey("parent")> _
    Public Property parent_id As Long
    <Xml.Serialization.XmlIgnore> _
        Public Property parent As parent
    

    【讨论】:

      猜你喜欢
      • 2018-02-01
      • 1970-01-01
      • 2013-11-24
      • 2020-03-21
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 2020-10-08
      • 2018-07-17
      相关资源
      最近更新 更多