2011-11-03 119 views
2

我有對象的基類所審覈:EF代碼優先映射覆雜類型關係

AuditableObject

public class AuditableObject : DomainObject, IAuditable 
{ 
    ... some fields 

    public AuditInfo AuditInfo 
    { 
     get; 
     set; 
    } 
} 

AuditInfo

public class AuditInfo : IAuditable 
{ 

    public int CreatedByDbId 
    { 
     get; 
     set; 
    } 

    public DateTime CreatedDate 
    { 
     get; 
     set; 
    } 

    public int? AmendedByDbId 
    { 
     get; 
     set; 
    } 

    public DateTime? AmendedDate 
    { 
     get; 
     set; 
    } 
} 

的CreatedByDbId和AmendedByDbId鏈接到SystemUser對象:

SystemUser

public class SystemUser 
{ 
    public int SystemUserDbId 
    { 
     get; 
     set; 
    } 

    public string Username 
    { 
     get; 
     set; 
    } 
} 

我有一個類呼叫從AuditableObject,其中也有其他SystemUser屬性繼承:

public class Call : AuditableObject 
{ 
    ... some fields 

    public SystemUser TakenBy { get; set;} 
    public SystemUser CreatedBy { get; set; } 
    public SystemUser CancelledBy { get; set;} 
    public int CancelledByDbId {get; set;} 
    public int TakenByDbId { get; set;} 
} 

調用數據庫表

CREATE TABLE [dbo].[Call](
[CallDbId] [int] IDENTITY(1,1) NOT NULL, 
[CancelledBy] [int] NULL, 
[CreatedByDbId] [int] NOT NULL, 
[CreatedDate] [datetime] NOT NULL, 
[AmendedByDbId] [int] NULL, 
[AmendedDate] [datetime] NULL, 
[TakenBy] [int] NOT NULL) 

我似乎無法獲得我的映射,例如

modelBuilder.ComplexType<AuditInfo>(); 
...// configuration for Call 
this.Property(x => x.AuditInfo.AmendedByDbId).HasColumnName("AmendedByDbId"); 
this.Property(x => x.AuditInfo.AmendedDate).HasColumnName("AmendedDate"); 
this.Property(x => x.AuditInfo.CreatedByDbId).HasColumnName("CreatedByDbId"); 
this.Property(x => x.AuditInfo.CreatedDate).HasColumnName("CreatedDate"); 

this.Property(t => t.CancelledByDbId).HasColumnName("CancelledBy"); 
this.Property(t => t.TakenByDbId).HasColumnName("TakenBy"); 

this.HasRequired(t => t.TakenBy).WithMany().HasForeignKey(x => x.TakenByDbId); 
this.HasRequired(t => t.CancelledBy).WithMany().HasForeignKey(x => x.CancelledByDbId); 

,我總是得到在運行時的錯誤,如:

Invalid column name 'SystemUser_SystemUserDbId'. 
Invalid column name 'SystemUser_SystemUserDbId1'. 
Invalid column name 'SystemUser_SystemUserDbId2'. 
Invalid column name 'CreatedBy_SystemUserDbId'. 

我無法弄清楚:(

回答

0

最後的錯誤「無效的列名稱CreatedBy_SystemUserDbId'」可能會出現,因爲缺少數據庫中CreatedBy導航屬性到CreatedByDbId外鍵列的映射。它應該如下所示:

this.HasRequired(t => t.CreatedBy) 
    .WithMany() 
    .Map(c => c.MapKey("CreatedByDbId")); 

我不知道其他三個錯誤的原因。

編輯

更仔細地看我有一種感覺,這種模式將是非常困難或者是不可能的映射。

  • 您對該類Call但在基類AuditableObject的複合型AuditInfo外鍵屬性CreatedByDbId導航屬性CreatedBy。我懷疑你可以在映射中定義這些屬性與SystemUser屬於同一關係。

  • 因此導航屬性CreatedBy應該在AuditInfo其中外鍵CreatedByDbId是。但這是不可能的,因爲您無法將導航屬性設置爲複雜類型。

  • 是否需要使用複雜類型AuditInfo?你能不能把它的性能直接進入AuditableObject

    public class AuditableObject : DomainObject, IAuditable 
    { 
        ... some fields 
    
        public int CreatedByDbId { get; set; } 
        public DateTime CreatedDate { get; set; } 
        public int? AmendedByDbId { get; set; } 
        public DateTime? AmendedDate { get; set; } 
    } 
    
  • 如果你這樣做,你應該能夠在CreatedBy導航屬性從Call實體移動到AuditableObject基類,其中FK CreatedByDbId生活,然後創建你已經做了其他兩個導航屬性相同的映射,但此時基類:

    modelBuilder.Entity<AuditableObject>() 
        .HasRequired(a => a.CreatedBy) 
        .WithMany() 
        .HasForeignKey(a => a.CreatedById);