我有一张FeeMetadata
带 PK的桌子FeeID BIGINT IDENTITY(1,1) NOT NULL
此表包含两个 1-1 关系。一到PFD.Fees
,一到SoaCourt.Fees
通过FeeID
列(三个表同名,只有元数据表标记为IDENTITY,其他两个表此列是PK但不是标识)
这是 EF 类的代码:
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
编辑:创建和持久化数据库实体的代码:
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
我遇到的问题是 SoaCourt.Fee 实体没有被持久化到数据库中。PFD.FeeMetadata 和 PFD.Fee 都正确保存,但 SoaCourt.Fee 不是。
关于如何解决这个问题的任何想法?