班级定义
Public Class AddressEntity
Private _addressId As Guid
Private m_strStreet As String
Private m_strSuite As String
Private m_strCity As String
Private m_State As String
Private m_strZipCode As String
Private m_strCountry As String
Public Overridable Property Street() As String
Get
Return m_strStreet
End Get
Set(ByVal Value As String)
m_strStreet = Left(Value, 50)
DataChanged(EntityState.Modified, "Street")
End Set
End Property
Public Overridable Property Suite() As String
Get
Return m_strSuite
End Get
Set(ByVal Value As String)
m_strSuite = Left(Value, 50)
DataChanged(EntityState.Modified, "Suite")
End Set
End Property
Public Overridable Property City() As String
Get
Return m_strCity
End Get
Set(ByVal Value As String)
m_strCity = Left(Value, 50)
DataChanged(EntityState.Modified, "City")
End Set
End Property
Public Overridable Property State() As String
Get
Return m_State
End Get
Set(ByVal Value As String)
m_State = Left(Value, 50)
DataChanged(EntityState.Modified, "State")
End Set
End Property
<MaxStringLength(10)>
Public Overridable Property ZipCode() As String
Get
Return m_strZipCode
End Get
Set(ByVal Value As String)
m_strZipCode = Left(Value, 10)
DataChanged(EntityState.Modified, "ZipCode")
End Set
End Property
Public Overridable Property Country() As String
Get
Return m_strCountry
End Get
Set(ByVal Value As String)
m_strCountry = Left(Value, 50)
DataChanged(EntityState.Modified, "Country")
End Set
End Property
<Key>
Public Overridable Property AddressId() As Guid
Get
Return _addressId
End Get
Set(ByVal value As Guid)
_addressId = value
End Set
End Property
Public Class AMAStatementEntity
Private _DateOfService As Nullable(Of DateTime)
Private _FacilityLocation As AddressEntity
Private _BillingAddress As AddressEntity
<Key>
Public Property AMAId() As Guid
Get
Return _amaId
End Get
Set(ByVal value As Guid)
_amaId = value
End Set
End Property
Public Property DateOfService() As Nullable(Of Date)
Get
Return _DateOfService
End Get
Set(ByVal value As Nullable(Of Date))
_DateOfService = value
End Set
End Property
'This was used in Castle Activerecord
'<Nested(ColumnPrefix:="FL")> _
Public Property FacilityLocation() As AddressEntity
Get
Return _FacilityLocation
End Get
Set(ByVal value As AddressEntity)
_FacilityLocation = value
End Set
End Property
'This was used in Castle Activerecord
'<Nested(ColumnPrefix:="BA")> _
Public Property BillingAddress() As AddressEntity
Get
Return _BillingAddress
End Get
Set(ByVal value As AddressEntity)
_BillingAddress = value
End Set
End Property
'Other irrelevant properties cut for brevity.
End Class
数据库表
CREATE TABLE [dbo].[AMA](
[AMAId] [uniqueidentifier] NOT NULL,
[DateOfService] [datetime] NULL,
[FLStreet] [nvarchar](255) NULL,
[FLSuite] [nvarchar](255) NULL,
[FLCity] [nvarchar](255) NULL,
[FLZipCode] [nvarchar](255) NULL,
[FLCountry] [nvarchar](255) NULL,
[FLState] [nvarchar](255) NULL,
[BAStreet] [nvarchar](255) NULL,
[BASuite] [nvarchar](255) NULL,
[BACity] [nvarchar](255) NULL,
[BAZipCode] [nvarchar](255) NULL,
[BACountry] [nvarchar](255) NULL,
[BAState] [nvarchar](255) NULL,
CONSTRAINT [PK_AMA] PRIMARY KEY CLUSTERED
(
[AMAId] ASC
)
) ON [PRIMARY]
鉴于上述类和表结构,我可以,如果是这样,我如何正确告诉Entity Framework这些类如何存储在数据库中?我正在从Castle ActiveRecord(位于NHibernate之上)迁移,它使用了Nested属性,带有选项列前缀。另外,我不知道这个构造的正确名称(如果有的话)是什么,我知道TPT,TPC和TPH,但是我没有找到这个名字。
答案 0 :(得分:1)
使用Entity Framework,您称之为“嵌套数据库类”的终端技术是"Complex Type"。
在EF术语中,它不是“实体”(它有自己的身份,在大多数情况下是自己的数据库表),因此名称AddressEntity
不是最好的,更好的是AddressType
也许
您将类本身标记为“ComplexType”,而不是包含类中的单个属性(例如您的FacilityLocation
):
<ComplexType>
Public Class AddressEntity
'...
默认情况下,EF会使用根据FacilityLocation
属性名称+下划线+ AddressEntity
的属性名称构建的列名称,例如
FacilityLocation_Street
FacilityLocation_Suite
不幸的是,EF不提供像我所知道的示例中的ColumnPrefix
映射这样简单的前缀重命名。您必须逐个映射属性,并且必须使用Fluent API执行此操作,无法通过数据注释实现此目的:
(在C#中):
modelBuilder.Entity<AMAStatementEntity>()
.Property(a => a.FacilityLocation.Street)
.HasColumnName("FLStreet");
modelBuilder.Entity<AMAStatementEntity>()
.Property(a => a.FacilityLocation.Suite)
.HasColumnName("FLSuite");
// etc.