实体框架1-1关系不是将数据保留到数据库
我有一张表 FeeMetadata
与PK FeeID BIGINT IDENTITY(1,1)NOT NULL
此表包含两个1-1关系。一个到 PFD.Fees
,一个到 SoaCourt.Fees
通过 FeeID
列(所有三个表中的同名,仅元数据表标记为IDENTITY,其他两个表此列为PK但不标识)
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)
这里是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
编辑:
创建和持久化数据库实体的代码:
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
我遇到的问题是SoaCourt.Fee实体没有被持久化到数据库。
PFD.FeeMetadata和PFD.Fee都正常保存,但是SoaCourt.Fee不是。
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.
总结:EF5不能从不同的命名空间保留同名的类。 实体类名称在命名空间中必须是唯一的。
In summary: EF5 can not persist classes of the same name from different namespaces. Entity class names must be unique across namespaces.