2

有人可以提供示例或解释如何使用 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; }
} 

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

EntityFramework.DLL 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

附加信息:无法确定类型“MedicDb.Patient.Medical.MedicalPatientDataEntity”和“MedicDb.Patient.Personal.PersonalPatientDataEntity”之间关联的主体端。此关联的主体端必须使用关系流式 API 或数据注释显式配置。

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

4

1 回答 1

2

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

谢谢大家

胡安·卡洛斯·加尔维斯

于 2012-08-09T13:04:47.177 回答