Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

There are a few questions out there but none of them have been much help to me so I decided to ask my own.

I want to use Entity Framework code first to create a strange relationship. I have three entities (Qualification, Test and Procedure) that all have the option of having a document(s). I am attempting to store all the documents in one table. How do I configure the relationship between all these entities.

  • Qualification has one to one optional Document
  • Test has one to one optional Document
  • Procedure has one to many optional Documents

Document has a required relationship to one Qualification, Test or Procedure.

public class Assessment
{
    public int AssessmentId { get; set; }
    //Other properties in Assessment
    public Document Document { get; set; }
}

public class Qualification
{
    public int QualificationId{ get; set; }
    //Other properties in Qualification
    public Document Document { get; set; }
}

public class Procedure
{
    public int ProcedureId { get; set; }
    //Other properties in Procedure
    public ICollection<Document> Documents { get; set; }
}

public class Document
{
    public int DocumentId { get; set; }
    //Other properties in Document
    public Qualification Qualification { get; set; }
    public Assessment Assessment { get; set; }
    public Procedure Procedure { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Not sure what goes here, but this was not working 
    modelBuilder.Entity<Document>()
        .HasOptional(d => d.Qualification).WithOptionalPrincipal(q => q.Document);

    modelBuilder.Entity<Document>()
        .HasOptional(d => d.Asssessment).WithOptionalPrincipal(q => q.Document);
}

When adding a migration I am getting an error:

Unable to determine the principal end of an association between the types. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

I am not sure how to do this. I had a nullable int DocumentId in Assessment and Qualification but that will not work b/c Procedure can hold a collection so Document must hold the Id. Does Document hold a nullable Id for each possible parent? Help please.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
2.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

In your mappings you set Document as principal, but in reality it is the dependent. So you should change your mappings to:

modelBuilder.Entity<Document>()
    .HasRequired(d => d.Qualification)
    .WithOptionalDependent(q => q.Document);

modelBuilder.Entity<Document>()
    .HasRequired(d => d.Asssessment)
    .WithOptionalDependent(q => q.Document);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...