johan Vikström
johan Vikström

Reputation: 1

DOT.NET System.Data.Entity.DbContext, mapping parent to child but not child to parent

I'm using npgsql.entityframework and due to certain aspects of the implementation of XmlSerializer I want a custom handling of the mapping.

Instead of standard method of mapping child to parent (as parent) I want the child to mapped using parent_id (as long).

I want to keep the function of adding child to parent in the model, but I want to be able handle child(s) without loading the parent.

Code in vb.net (answers is c# or vb)

Example parent add: dim p as new parent p.child.add( new child )

Example child add without parent loaded, but parent id known, dim c as new child c.parent_id= known_parent_id

Any help much appreciated.

Public Class parent

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

End Class

Public Class child

<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

End Class

/johan

Upvotes: 0

Views: 127

Answers (1)

johan Vikstr&#246;m
johan Vikstr&#246;m

Reputation: 1

found a solution,using ForeignKey in child mapping to parent enabled use of both methods.

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

Upvotes: 0

Related Questions