I’ve got a table FeeMetadata with a PK FeeID BIGINT IDENTITY(1,1) NOT NULL
This table contains two 1-1 relationships. One to PFD.Fees, and one to SoaCourt.Fees via the FeeID column (same name in all three tables, only the metadata table is marked as IDENTITY, the other two tables this column is PK but NOT identity)
Here’s the code for the EF classes:
Namespace PFD
<Table("FeeMetadata", Schema:="PFD")>
Public Class FeeMetadata
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal tFee As SOACourt_v1)
Me.New()
Me.GroupKey = tFee.DriverLicenseNumber
Me.PfdFee = New PFD.Fee(tFee)
Me.SoaCourtFee = New SoaCourt.Fee(tFee)
End Sub
<Key>
<DatabaseGenerated(DatabaseGeneratedOption.Identity)>
Public Property FeeID As Int64
' Other domain-specific properties...
Public Property SoaCourtFee As SoaCourt.Fee
End Class
End Namespace
Namespace PFD
<Table("Fees", Schema:="PFD")>
Public Class Fee
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal tFee As SOACourt_v1)
Me.New()
Me.Amount = tFee.Amount
Me.DueDate = tFee.DueDate
End Sub
<Key>
<ForeignKey("MetaData")>
<DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property FeeID As Int64
' Other domain-specific properties...
Public Property MetaData As FeeMetadata
End Class
End Namespace
Namespace SoaCourt
<Table("Fees", Schema:="SoaCourt")>
Public Class Fee
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal tFee As SOACourt_v1)
Me.New()
Me.CaseID = tFee.CaseID
Me.CaseNumber = tFee.CaseNumber
Me.TicketNumber = tFee.TicketNumber
End Sub
<Key>
<ForeignKey("MetaData")>
<DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property FeeID As Int64
' Other domain-specific properties
Public Property MetaData As PFD.FeeMetadata
End Class
End Namespace
EDIT:
Code to create and persist the database entities:
Using tContext As FeesContext = New FeesContext
For Each tFee As SOACourt_v1 In tFees
tContext.FeeMetadata.Add(New PFD.FeeMetadata(tFee))
Next
tContext.SaveChanges()
End Using
The problem I am having is that the SoaCourt.Fee entities are not being persisted to the database.
PFD.FeeMetadata and PFD.Fee are both saving properly, but SoaCourt.Fee is NOT.
Any thoughts on how to solve this?
The answer to this question is the solution here.
In summary: EF5 can not persist classes of the same name from different namespaces. Entity class names must be unique across namespaces.