Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For both your questions Q1 and Q2 you will need two roundtrips/two calls to <code>SaveChanges</code> (aside from solving the problem with a Stored Procedure maybe).</p> <p>Q1: The first call having <code>Question.CorrectAnswer</code> set to <code>null</code> and a second call that sets the <code>CorrectAnswer</code> to one of the stored answers.</p> <p>Q2: The first call setting <code>Question.CorrectAnswer</code> to <code>null</code> and the second call deletes the <code>Question</code> and the related answers with enabled cascading delete.</p> <p>If you are not so worried about the two roundtrips but more about the two transactions corresponding to the two <code>SaveChanges</code> calls you can wrap the whole operation including the two <code>SaveChanges</code> calls into a single manual transaction. (Example: <a href="https://stackoverflow.com/questions/12809958/ef-how-do-i-call-savechanges-twice-inside-a-transaction">EF: How do I call SaveChanges twice inside a transaction?</a>)</p> <p>About the one-to-one relationship: Although from business perspective the relationship for the <code>CorrectAnswer</code> is one-to-one it is hard or even impossible to model it as a one-to-one relationship with EF.</p> <p>The problem is that EF does not support foreign key one-to-one associations, i.e. relationships where the foreign key (CorrectAnswerId or so) has a unique constraint. It only supports shared primary key one-to-one associations where the primary key of the dependent (<code>Question</code>) is the foreign key (for <code>Question.CorrectAnswer</code>) to the principal (<code>Answer</code>) at the same time. Your Fluent code is the configuration for such a shared primary key association. But it would mean that the only valid <code>CorrectAnswer</code> is the <code>Answer</code> that has the same primary key value as the <code>Question</code>. While this is theoretically possible to achieve (your <code>Answer</code> table has more records than the <code>Question</code> table) it would most likely require to not use autogenerated keys but supply the keys manually. Changing the <code>CorrectAnswers</code> from one <code>Answer</code> to the other would be impossible. So, shared primary keys are not suited for your model in my opinion.</p> <p>The better solution would be to remove your Fluent mapping. The result would be a one-to-many relationship with a nullable foreign key for <code>CorrectAnswer</code> in the <code>Question</code> table. From database perspective it means that the same <code>Answer</code> can be the <code>CorrectAnswer</code> for many <code>Question</code>s which is probably nonsense in your business logic because every question has its own unique set of answers and two questions never share the same answer. But you can "hide" this one-to-many relationship from your business logic by just not adding an inverse collection property (like <code>QuestionsThisIsTheCorrectAnswerFor</code>) to <code>Answer</code>. Although it does not model the business constraint perfectly it works technically without problems. </p> <p>More about the difficulties of one-to-one relationships with EF can be found in these blog posts:</p> <ul> <li><a href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" rel="nofollow noreferrer">Shared Primary Key One-to-One Associations</a></li> <li><a href="http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx" rel="nofollow noreferrer">One-to-One Foreign Key Associations</a></li> </ul>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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