2012-04-11 103 views
0

我有一個TPH映射的問題。 這裏是類:C#,實體框架,TPH

摘要服務(基類)

[Table("Services")] 
public abstract class AbstractService : IAuditedObject 
{ 
    public int Id { get; set; } 

    [DisplayName("Receiver Site")] 
    public int? TargetSiteId { get; set; } 
    [DisplayName("Receiver Site")] 
    public virtual Site TargetSite { get; set; } 

    [DisplayName("Start Date")] 
    public PartialDate StartDate { get; set; } 
    [DisplayName("End Date")] 
    public PartialDate EndDate { get; set; } 

    [DisplayName("Study")] 
    public int? StudyId { get; set; } 
    [DisplayName("Study")] 
    public virtual Study Study { get; set; } 
} 

混凝土服務

public class AssociatedStaffService : AbstractService 
{ 
    [DisplayName("Person")] 
    [Required] 
    public int? SourcePersonId { get; set; } 
    [DisplayName("Person")] 
    public virtual Person SourcePerson { get; set; } 

    [DisplayName("Service")] 
    [Required] 
    public int? RoleId { get; set; } 
    [DisplayName("Service")] 
    public virtual AssociatedStaffServiceCLI Role { get; set; } 

    [DisplayName("Department")] 
    public string Department { get; set; } 

    public bool IsActive() 
    { 
     return this.SourcePerson != null && this.TargetSite != null && this.SourcePerson.IsActive() && this.TargetSite.IsActive() && this.Study != null && (this.EndDate == null || this.EndDate.Date == null || this.EndDate.Date > DateTime.Now); 
    } 
} 

public class EthicCommitteeService : AbstractService 
{ 
    [DisplayName("Site")] 
    [Required] 
    public int? SourceSiteId { get; set; } 
    [DisplayName("Site")] 
    public virtual Site SourceSite { get; set; } 

    [DisplayName("Central")] 
    public bool? IsCentral { get; set; } 

    public bool IsActive() 
    { 
     return this.Study != null && this.TargetSite != null && this.SourceSite != null && this.TargetSite.IsActive() && this.SourceSite.IsActive() && (this.EndDate == null || this.EndDate.Date == null || this.EndDate.Date > DateTime.Now); 
    } 
} 

public class ParticipatingService : AbstractService 
{ 
    public const string AUTHORIZATION_DATE = "AuthorizationDate"; 
    public const string IS_NATIONAL_COORDINATOR = "IsNationalCoordinator"; 

    [DisplayName("Person")] 
    [Required] 
    public int? SourcePersonId { get; set; } 
    [DisplayName("Person")] 
    public virtual Person SourcePerson { get; set; } 

    [DisplayName("Service")] 
    [Required] 
    public int? RoleId { get; set; } 
    [DisplayName("Service")] 
    public virtual ParticipatingServiceCLI Role { get; set; } 

    [DisplayName("Department")] 
    public string Department { get; set; } 

    public int? RegInvestigatorFormId { get; set; } 
    public PartialDate AuthorizationDate { get; set; } 
    public bool? IsNationalCoordinator { get; set; } 

    public bool IsActive() 
    { 
     return this.SourcePerson != null && this.TargetSite != null && this.SourcePerson.IsActive() && this.TargetSite.IsActive() && this.Study != null && (this.EndDate == null || this.EndDate.Date == null || this.EndDate.Date > DateTime.Now); 
    } 
} 

public class ExternalService : AbstractService 
{ 
    [DisplayName("Person")] 
    [Required] 
    public int? SourcePersonId { get; set; } 
    [DisplayName("Person")] 
    public virtual Person SourcePerson { get; set; } 

    [DisplayName("Service")] 
    [Required] 
    public int RoleId { get; set; } 
    [DisplayName("Service")] 
    public virtual ExternalServiceCLI Role { get; set; } 

    [DisplayName("Department")] 
    public string Department { get; set; } 

    public bool IsActive() 
    { 
     return this.SourcePerson != null && this.TargetSite != null && this.SourcePerson.IsActive() && this.TargetSite.IsActive() && this.Study != null && (this.EndDate == null || this.EndDate.Date == null || this.EndDate.Date > DateTime.Now); 
    } 
} 

public class StudyTeamService : AbstractService 
{ 
    [DisplayName("Person")] 
    [Required] 
    public int? SourcePersonId { get; set; } 
    [DisplayName("Person")] 
    public virtual Person SourcePerson { get; set; } 

    [DisplayName("Service")] 
    [Required] 
    public int? RoleId { get; set; } 
    [DisplayName("Service")] 
    public virtual StudyTeamServiceCLI Role { get; set; } 

    [DisplayName("Department")] 
    public string Department { get; set; } 

    public bool IsActive() 
    { 
     return this.SourcePerson != null && this.TargetSite != null && this.SourcePerson.IsActive() && this.TargetSite.IsActive() && this.Study != null && (this.EndDate == null || this.EndDate.Date == null || this.EndDate.Date > DateTime.Now); 
    } 
} 

我總是得到以下錯誤:

--- ERROR: One or more validation errors were detected during model generation:

System.Data.Edm.EdmProperty: Name: Each property name in a type must be unique. Property name 'StartDate' is already defined. 
    System.Data.Edm.EdmProperty: Name: Each property name in a type must be unique. Property name 'EndDate' is already defined. --- 

這裏是堆棧跟蹤:

Unhandled Exception: System.Data.Entity.ModelConfiguration.ModelValidationExcept ion: One or more validation errors were detected during model generation:

System.Data.Edm.EdmProperty: Name: Each property name in a type must be unique. Property name 'StartDate' is already defined. 
    System.Data.Edm.EdmProperty: Name: Each property name in a type must be unique. Property name 'EndDate' is already defined. 

at Prisma.Loader.PrismaLoader.Init() in C:\Projects_PrismaLoader\Prisma.Load er\PrismaLoader.cs:line 95 at Prisma.Loader.PrismaLoader.Load(Boolean quick) in C:\Projects_PrismaLoade r\Prisma.Loader\PrismaLoader.cs:line 32 at Prisma.Loader.Program.Main(String[] args) in C:\Projects_PrismaLoader\Pri sma.Loader\Program.cs:line 113

堆棧跟蹤是不是在這種情況下,真正有用的(似乎...)

有誰有,我犯了一個錯誤的想法?我一直在尋找一天...

+0

你能告訴我什麼是'IAuditedObject'嗎? – Likurg 2012-04-11 13:22:16

+0

IAuditedObject是一個空的接口,它允許我們記錄在對象上完成的每個更改,以便我們可以隨時重新創建它。 (= Trailed對象) – Whoami 2012-04-11 13:37:20

回答

0

解決

看來這個問題是PartialDate類。它必須用[ComplexType]註釋進行註釋。

+0

嗨,你如何實現這些實體的服務?你有針對每個實體的所有操作或任何常用的實現它們的操作? – Patrick 2013-01-06 14:56:13

+0

@Patrick你的服務意味着什麼?如果您正在討論可以針對數據庫引擎完成的操作,我們目前使用通用存儲庫/管理器來提供這些操作。 – Whoami 2013-01-08 07:10:40

+0

我正在談論一個TPH類的關係的CRUD操作,例如一個Insert,我怎樣才能在數據庫和ExternalService字段中插入公共字段(AbstractService),而不違反DRY原則? – Patrick 2013-01-08 10:23:58

0

我相信你需要使StartDate和EndDate虛擬? Edm試圖覆蓋功能,但是因爲它們被封閉了,所以它不能覆蓋它們,所以它在IL中複製它們?

+0

似乎沒有,只是試圖將它們設置爲虛擬的,仍然是相同的錯誤。但謝謝你的嘗試:) – Whoami 2012-04-11 13:36:04