We're trying to map some redundant relationships, and have been unable to figure out the perfect combination of foreign keys, navigational properties, and ModelBuilder statements.
This is a simplified example; I realize that some people might recommend changing the table structure as a whole, but we've encountered several situations where we need this type of configuration.
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
Model Builder Attempt #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)
Usage:
Dim NewCustomer As Customer 'Assigned Elsewhere'
Dim NewContact As Contact 'Assigned Elsewhere'
NewCustomer.PrimaryContact =
New PrimaryContact With {
.Contact = NewContact
}
The Problems:
Our problems lie with the PrimaryContact table and relationships/navigational properties.
The current problem is that NewCustomer.PrimaryContact.Customer
has no reference after this assignment.
There is a secondary problem where even if we manually assign NewCustomer.PrimaryContact.Customer = Customer
, there is a valid reference, but EF does not wire up the ContactID
on the insert
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
Solutions!?
What advice do you have on how to properly configure the entities, or change the ModelBuilder mapping so that these entities and relationships function as we expect them to.
Upon further brainstorming, I realized that the navigational property from Contacts to PrimaryContact would never actually be useful. It turns out it was not useful and actually the culprit. When I removed it and the corresponding ModelBuilder code, everything started working
POCO Class
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 ------------------'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.