EF 5RC Table splitting into more than two entities

2019-05-27 12:47发布

Can someone provide an example or explain how can I split a table into more than two entities using data annotations on EF 5 RC Code First?

I have 4 entities I want to be mapped into just one table. The code for each one of them are:

[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; }

} 

And my context is this:

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; }
} 

When I run that I receive the following exception:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.DLL but was not handled in user code

Additional information: Unable to determine the principal end of an association between the types 'MedicDb.Patient.Medical.MedicalPatientDataEntity' and 'MedicDb.Patient.Personal.PersonalPatientDataEntity'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

What am I doing wrong here? I really need some help here.

1条回答
Fickle 薄情
2楼-- · 2019-05-27 13:40

Well, I fixed the problem, I guess the main cause is my ignorance on the topic, the [ForeignKey] attribute must be at just one end of the relation, that fix the exception but to make the 4 entities be on the same table there must be a one to one relation between the 4 of them, it looks a bit unnatural but that's the only way it works. So, at the end I'm not splitting my table but, instead, creating 4 separated tables having one to one relation not among the 4 of them but between the PatientDataEntity and each one of the other 3, that is done by removing the [ForeingKey] annotation from the properties in PatientDataEntity but leaving them on the other 3 entities (on the navigation properties toward PatientDataEntity. That´s all. That in case orther people arrives at the same problem.

Thanks to all

Juan Carlos Galvez

查看更多
登录 后发表回答