在Entity Framework中实现“嵌套”属性

时间:2013-05-16 12:39:19

标签: entity-framework-5 castle-activerecord complextype

编辑: 看起来这在实体框架中被称为“复杂类型”,我看到我如何能够在列上设置名称,以便在类中包含复杂类型,但我不知道如何/如果可以它包含类中包含的多个相同复杂类型。

http://blogs.msdn.com/b/wriju/archive/2010/07/26/ef4-complex-type.aspx

EntityFramework 4.1 Code First incorrectly names complex type column names


由于Castle ActiveRecord已经sunset,我正在转向Entity Framework 5,但是我不知道如何完成Activerecord的“嵌套属性”概念:

http://docs.castleproject.org/Active%20Record.Mappings.ashx#Nested_5

http://docs.castleproject.org/Default.aspx?Page=Nested%20data%20%28NHibernate%20components%29&NS=Active%20Record&AspxAutoDetectCookieSupport=1

我已经完成了我的谷歌搜索,但我不确定“嵌套”是EF中这个概念的正确术语,如果它甚至存在的话。以下是我在Castle ActiveRecord中的表格和类,有人可以提供EF术语以及如何实现:

标有<Nested(ColumnPrefix:="FL")))>的AmaStatementEntity上的Address属性映射到数据库列FLStreet,FLSuite,FLCity等。

CREATE TABLE [dbo].[AMA](
--other columns not necessary to discussion removed
    [AMAId] [uniqueidentifier] NOT 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,
    [PAStreet] [nvarchar](255) NULL,
    [PASuite] [nvarchar](255) NULL,
    [PACity] [nvarchar](255) NULL,
    [PAZipCode] [nvarchar](255) NULL,
    [PACountry] [nvarchar](255) NULL,
    [PAState] [nvarchar](255) NULL,
    [IAStreet] [nvarchar](255) NULL,
    [IASuite] [nvarchar](255) NULL,
    [IACity] [nvarchar](255) NULL,
    [IAZipCode] [nvarchar](255) NULL,
    [IACountry] [nvarchar](255) NULL,
    [IAState] [nvarchar](255) NULL,
    [EAStreet] [nvarchar](255) NULL,
    [EASuite] [nvarchar](255) NULL,
    [EACity] [nvarchar](255) NULL,
    [EAZipCode] [nvarchar](255) NULL,
    [EACountry] [nvarchar](255) NULL,
    [EAState] [nvarchar](255) NULL,
    [PYAStreet] [nvarchar](255) NULL,
    [PYASuite] [nvarchar](255) NULL,
    [PYACity] [nvarchar](255) NULL,
    [PYAZipCode] [nvarchar](255) NULL,
    [PYACountry] [nvarchar](255) NULL,
    [PYAState] [nvarchar](255) NULL,
 CONSTRAINT [PK_AMA] PRIMARY KEY CLUSTERED 
(
    [AMAId] ASC
))

    Public Class AmaStatementEntity

        Public Sub New()
            _amaId = Guid.NewGuid
        End Sub
'Other properties not relavent to discussion removed

        <Nested(ColumnPrefix:="FL")))>
        Public Property FacilityLocation() As AddressEntity

        <Nested(ColumnPrefix:="BA"))>
        Public Property BillingAddress() As AddressEntity

        <Nested(ColumnPrefix:="PA"))>
        Public Property PatientAddress() As AddressEntity

        <Nested(ColumnPrefix:="IA"))>
        Public Property InsuredAddress() As AddressEntity

        <Nested(ColumnPrefix:="EA"))>
        Public Property EmployerAddress() As AddressEntity

        <Nested(ColumnPrefix:="PYA"))>
        Public Property PayorAddress() As AddressEntity

        Public Property AMAId() As Guid
    End Class

Public Class AddressEntity
    Public Property Street() As String
    Public Property Suite() As String
    Public Property City() As String
    Public Property State() As String
    Public Property ZipCode() As String
    Public Property Country() As String
    Public Property AddressId() As Guid
End Class

1 个答案:

答案 0 :(得分:1)

一旦找到了正确的术语,我就能找到答案:

Entity Framework - Reuse Complex Type

对于我的特定场景,这是你将如何做到的(例子显示了其中一个comlex类型引用的映射):

        modelBuilder.ComplexType<AddressEntity>();
        modelBuilder.Entity<AmaStatementEntity>().ToTable("vAMA");
        modelBuilder.Entity<AmaStatementEntity>().HasKey(a => a.AMAId);
        modelBuilder.Entity<AmaStatementEntity>().Property(a => a.FacilityLocation.Street).HasColumnName("FLStreet");
        modelBuilder.Entity<AmaStatementEntity>().Property(a => a.FacilityLocation.Suite).HasColumnName("FLSuite");
        modelBuilder.Entity<AmaStatementEntity>().Property(a => a.FacilityLocation.City).HasColumnName("FLCity");
        modelBuilder.Entity<AmaStatementEntity>().Property(a => a.FacilityLocation.ZipCode).HasColumnName("FLZipCode");
        modelBuilder.Entity<AmaStatementEntity>().Property(a => a.FacilityLocation.Country).HasColumnName("FLCountry");
        modelBuilder.Entity<AmaStatementEntity>().Property(a => a.FacilityLocation.State).HasColumnName("FLState");