Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to solve combined one-to-one and one-to-many relationship in EF 5 Code First
    primarykey
    data
    text
    <p>I´m using Entity Framework 5 and Code First.</p> <p>I have two domain entities <strong>Question</strong> and <strong>Answer</strong> for a quiz application. One question has several possible answers. A question also has one correct answer which should reference one of the possible answers. I am experiencing some issues with the combination of a one-to-many and one-to-one relationship between the to entities. See <strong>Q1</strong> and <strong>Q2</strong>.</p> <p>This is the code for the entities:</p> <pre><code>public class Question { public virtual int Id { get; set; } [Required] public virtual string Text { get; set; } [InverseProperty("Question")] public virtual ICollection&lt;Answer&gt; PossibleAnswers { get; set; } public virtual Answer CorrectAnswer { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public virtual DateTime? UpdateStamp { get; set; } } public class Answer { public virtual int Id { get; set; } [Required] public virtual string Text { get; set; } [ForeignKey("QuestionId")] public virtual Question Question { get; set; } public virtual int QuestionId { get; set; } } </code></pre> <p><strong>Q1:</strong> What should I do to be able to insert the Question object and referenced Answers (thru property PossibleAnswers) in just one roundtrip to the db (eg one call to the contexts SaveChanges)? The error I get when I save the Questions and Answers without adding the Answers first is:</p> <blockquote> <p>Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.</p> </blockquote> <p>To solve that problem I tried the following using fluent API to get the Answers to be added prior to Questions when doing it all with just one call the objectcontexts SaveChanges:</p> <pre><code> protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity&lt;Question&gt;() .HasOptional(q =&gt; q.CorrectAnswer) .WithRequired(); base.OnModelCreating(modelBuilder); } </code></pre> <p>However, that lead me to another error:</p> <blockquote> <p>Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.</p> </blockquote> <p>Am I on the right path with the fluent API approach for Q1? Why the error message?</p> <p><strong>Q2</strong>: When deleting a question I realize that there will be an error since the question cannot be deleted before the answers and vice versa. How do I solve this? For instance, is WillCascadeOnDelete supposed to be specified on both Question.CorrectAnswer and Question.PossibleAnswers?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload