Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework One-To-Many Delete relationship
    primarykey
    data
    text
    <p>I have two entities <code>Transaction</code> and <code>DiscountType</code></p> <p><strong>Transaction:</strong></p> <pre><code>public class Transaction { [Key] public int TransactionID { get; set; } public virtual DiscountType DiscountType { get; set; } } </code></pre> <p><strong>DiscountType:</strong></p> <pre><code>public class DiscountType { [Key] public int DiscountTypeID { get; set; } public virtual ICollection&lt;Transaction&gt; Transactions { get; set; } public float DiscountValue { get; set; } public bool Enabled {get; set;} } </code></pre> <p><strong>Relationship:</strong></p> <pre><code> modelBuilder.Entity&lt;DiscountType&gt;().HasMany(d =&gt; d.Transactions); </code></pre> <p><strong>Aim:</strong></p> <p>I am trying to remove the relationship between the Transaction and the DiscountType, without actually removing either entity.</p> <p><strong>Problem:</strong></p> <p>Now I'm having no problem assigning a Discount to a transaction, but when I do the following:</p> <pre><code> var transaction = context.Transactions.Where(t =&gt; t.TransactionID == 1).First(); transaction.DiscountType = context.DiscountTypes.Where(d =&gt; d.DiscountTypeID == 1).First(); context.SaveChanges(); var _transaction = context.Transactions.Where(t =&gt; t.TransactionID == 1).First(); _transaction.DiscountType = null; context.SaveChanges(); </code></pre> <p>It's quite a peculiar problem I'm having. When this code is ran it does not remove the relationship between the two.</p> <p><strong>Actual Code:</strong></p> <p><img src="https://i.stack.imgur.com/FrLLu.png" alt="As you can see, even after the &lt;code&gt;= null;&lt;/code&gt; is executed, it still remains."></p> <p>As you can see, even after the <code>= null;</code> is executed, it still remains.</p>
    singulars
    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.
    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