EF 5RC表拆分为两个以上的实体

时间:2012-08-08 15:16:33

标签: entity-framework ef-code-first split

有人可以提供示例或解释如何使用EF 5 RC Code First上的数据注释将表拆分为两个以上的实体吗?

我想要将4个实体映射到一个表中。每个代码都是:

[Table("PatientDataEntities")]
public class PatientDataEntity
{
    [Key]
    [ForeignKey("UserFullName")]
    public virtual Guid Id { get; set; }

    public virtual UserFullNameEntity UserFullName { get; set; }

    public virtual GeneralData GenData {get; set;}

    [ForeignKey("Id")]
    public virtual PersonalPatientDataEntity PersonalData { get; set; }

    [ForeignKey("Id")]
    public virtual MedicalPatientDataEntity MedicalData { get; set; }

    [ForeignKey("Id")]
    public virtual FinancialPatientDataEntity FinancialData { get; set; }

} 

[Table("PatientDataEntities")]
public class PersonalPatientDataEntity
{
    [Key]
    [ForeignKey("UserFullName")]
    public virtual Guid Id { get; set; }

    public virtual UserFullNameEntity UserFullName { get; set; }

    [ForeignKey("Id")]
    public virtual MedicalPatientDataEntity MedicalPatientData { get; set; }

    [ForeignKey("Id")]
    public virtual FinancialPatientDataEntity FinancialPatientData { get; set; }

    [ForeignKey("Id")]
    public virtual PatientDataEntity PatientData { get; set; }

    [DataType(DataType.DateTime)]
    public virtual DateTime Birthdate { get; set; }

    public virtual Gender Sex { get; set; }

    public virtual MaritalStatus MStatus { get; set; }

    public virtual byte Children { get; set; }

    public virtual string Education { get; set; }

    public string Profession { get; set; }

} 


[Table("PatientDataEntities")]
public class MedicalPatientDataEntity
{
    [Key]
    [ForeignKey("UserFullName")]
    public virtual Guid Id { get; set; }

    public virtual UserFullNameEntity UserFullName { get; set; }

    [ForeignKey("Id")]
    public virtual PersonalPatientDataEntity PersonalData { get; set; }

    [ForeignKey("Id")]
    public virtual FinancialPatientDataEntity FinancialData { get; set; }

    [ForeignKey("Id")]
    public virtual PatientDataEntity PatientData { get; set; }

    public virtual string ClinicalHistoryNumber { get; set; }

    public virtual BiologicalState State { get; set; }

    public virtual Guid PhysicianId { get; set; }

    public virtual DateTime? RegisterDate { get; set; }

    public virtual bool AcceptsDataTreatment { get; set; }

    public virtual bool AcceptsImageTreatment { get; set; }

    public virtual /*List<Allergy>*/ string Allergies { get; set; }

    public virtual /*List<Surgery>*/ string Surgeries { get; set; }

    public virtual /*List<Medication>*/ string Medications { get; set; }

    public virtual /*List<MedicalPrecedent>*/ string MedicalPrecedents { get; set; }

    [DataType(DataType.MultilineText)]
    public virtual string Family { get; set; }

    public virtual string Nicotinism { get; set; }

    public virtual float? Weight { get; set; }

    public virtual float? Height { get; set; }

    [NotMapped]
    public virtual float? BodyMassIndex { get; set; }

    public virtual string Remarks { get; set; }
} 


[Table("PatientDataEntities")]
public class FinancialPatientDataEntity
{
    [Key]
    [ForeignKey("UserFullName")]
    public virtual Guid Id { get; set; }

    public virtual UserFullNameEntity UserFullName { get; set; }

    [ForeignKey("Id")]
    public virtual PersonalPatientDataEntity PersonalData { get; set; }

    [ForeignKey("Id")]
    public virtual MedicalPatientDataEntity MedicalData { get; set; }

    [ForeignKey("Id")]
    public virtual PatientDataEntity PatientData { get; set; }

    public virtual string KindOfPatient { get; set; }

    public virtual string Isapre { get; set; }

} 

我的背景是:

public class MedicDbContext : DbContext
{
    public DbSet<UserFullNameEntity> UsersFullName { get; set; }
    public DbSet<MedicDb.Patient.PatientDataEntity> PatientDataEntities { get; set; }

    public DbSet<MedicDb.Patient.Personal.PersonalPatientDataEntity> PersonalPatientDataEntities { get; set; }

    public DbSet<MedicDb.Patient.Financial.FinancialPatientDataEntity> FinancialPatientDataEntities { get; set; }

    public DbSet<MedicDb.Patient.Medical.MedicalPatientDataEntity> MedicalPatientDataEntities { get; set; }
} 

当我运行时,我收到以下异常:

  

发生了'System.InvalidOperationException'类型的异常   EntityFramework.DLL但未在用户代码中处理

     

其他信息:无法确定主要结尾   类型之间的关联   'MedicDb.Patient.Medical.MedicalPatientDataEntity'和   'MedicDb.Patient.Personal.PersonalPatientDataEntity'。校长   必须使用以下任一方式显式配置此关联的结尾   关系流畅的API或数据注释。

我在这里做错了什么?我真的需要一些帮助。

1 个答案:

答案 0 :(得分:2)

好吧,我解决了这个问题,我猜主要原因是我对这个主题一无所知,[ForeignKey]属性必须只是关系的一端,修复异常但要使4个实体在同一个表中它们中的4个之间必须存在一对一的关系,它看起来有点不自然,但这是它工作的唯一方式。所以,最后我不是拆分我的表,而是创建4个分开的表,其中一对一关系不在其中4个中,而是在PatientDataEntity和其他3个中的每一个之间,这是通过删除[来自PatientDataEntity属性的ForeingKey]注释,但将它们留在其他3个实体上(在PatientDataEntity的导航属性上)。 就这样。 如果其他人遇到同样的问题。

感谢所有

Juan Carlos Galvez