Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

c# - Entity Framework table with multiple optional one to one relationships

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...