How to map redundant relationships in Entity Framework (EF6) Code-First

We are trying to match some redundant relationships and were unable to determine the perfect combination of foreign keys, navigation properties, and ModelBuilder operators.

This is a simplified example; I understand that some people may recommend changing the structure of the table as a whole, but we are faced with several situations in which we need this type of configuration.

Simplified scheme: Simplified Example Scheme

POCO Classes:

Public Class Customer
    <Key, Required>
    Public Property CustomerID As Int32

    Public Property Contacts As List(of Contact)

    Public Property PrimaryContact As PrimaryContact


Public Class Contact

    <Key, Required>
    Public Property ContactID As Int32

    <Required>
    Public Property CustomerID As Int32

    Public Property Customer As Customer

    Public Property PrimaryContact As PrimaryContact



Public Class PrimaryContact

    <Key, Required>
    Public Property CustomerID As Int32

    <Required>
    Public Property ContactID As Int32

    Public Property Customer as Customer

    Public Property Contact as Contact

Attempt to create model # 1:

modelBuilder.Entity(Of Customer
).HasOptional(Function(C) C.PrimaryContact
).WithRequired(Function(PC) PC.Customer)

modelBuilder.Entity(Of Contact
).HasOptional(Function(C) C.PrimaryContact
).WithRequired(Function(PC) PC.Contact)

Application:

Dim NewCustomer As Customer   'Assigned Elsewhere'
Dim NewContact As Contact     'Assigned Elsewhere'

NewCustomer.PrimaryContact =
    New PrimaryContact With {
        .Contact = NewContact 
    }

Problems:

Our problems lie in the PrimaryContact table and relations / navigational properties.

The current problem is that it NewCustomer.PrimaryContact.Customerdoes not have a link after this assignment.

, NewCustomer.PrimaryContact.Customer = Customer, , EF ContactID

insert into "[SCHEMA]"."PRIMARYCONTACTS"("CUSTOMERID", "CONTACTID")
values (:p0, :p1)

-- :p0: '197467' (Type = Int32)

-- :p1: '0' (Type = Int32)

-- Executing at 9/1/2015 2:57:45 PM -04:00

-- Failed in 514 ms with error: ORA-02291: integrity constraint ([SCHEMA].PRIMARYCONTACTS_FK2) violated - parent key not found

!

ModelBuilder, , .

+4
1

, Contacts to PrimaryContact . , . ModelBuilder,

POCO

Public Class Contact

    <Key, Required>
    Public Property ContactID As Int32

    <Required>
    Public Property CustomerID As Int32

    Public Property Customer As Customer

    '-------------------- REMOVED  --------------------'
    '                                                  '
    ' Public Property PrimaryContact As PrimaryContact '
    '                                                  '
    '-------------------- REMOVED  --------------------'

Model Builder

modelBuilder.Entity(Of Customer
).HasOptional(Function(C) C.PrimaryContact
).WithRequired(Function(PC) PC.Customer)

'------------------ REMOVED  ------------------'
'                                              '        
' modelBuilder.Entity(Of Contact               '
' ).HasOptional(Function(C) C.PrimaryContact   '
' ).WithRequired(Function(PC) PC.Contact)      '
'                                              '
'------------------ REMOVED  ------------------'        
+1

All Articles