有人可以提供示例或解释如何使用 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 或数据注释显式配置。
我在这里做错了什么?我真的需要一些帮助。