1

类定义

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 之上)迁移,它使用了一个带有选项列前缀的嵌套属性。另外,我不知道这个构造的正确名称(如果有的话)是什么,我知道 TPT、TPC 和 TPH,但我还没有找到它的名称。

4

1 回答 1

1

使用实体框架,您所谓的“嵌套数据库类”的终点技术是复杂类型

在 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.
于 2013-01-14T20:03:39.860 回答