Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think your problem is happening because you're creating a new <code>MedicineInventory</code>.</p> <p>Replace this:</p> <pre><code>MedicineInventory toRemove = new MedicineInventory(); </code></pre> <p>With this:</p> <pre><code>var toRemove = ctx.MedicineInventories.Single(mi =&gt; mi.MedicineInventoryId == Mid); </code></pre> <p><strong>Update:</strong></p> <p>When it retuns the error message <em>"Sequence contains no elements"</em> it's because EF could not find a row in the database with the same Guid you're using in the <code>where</code> clause. In this case and to avoid an exception, you can try this line of code:</p> <pre><code>var toRemove = ctx.MedicineInventories.SingleOrDefault( mi =&gt; mi.MedicineInventoryId == Mid); </code></pre> <p>then use an <code>if</code> to delete if it's not <code>NULL</code>:</p> <pre><code>if(toRemove != null) { ctx.MedicineInventories.Remove(toRemove); ctx.SubmitChanges(); } else { // Only you know what to do! :-) } </code></pre> <blockquote> <p><a href="http://msdn.microsoft.com/en-us/library/bb342451.aspx" rel="nofollow">SingleOrDefault</a> returns the only element of a sequence, or a default value (<code>NULL</code> in this case) if the sequence is empty; this method throws an exception if there is more than one element in the sequence.</p> </blockquote> <hr> <p>Note: the way you're comparing the Guids is correct because <code>==</code> is overloaded on Guid so you don't need to compare the string representations.</p> <p>See <a href="http://msdn.microsoft.com/en-us/library/system.guid.op_equality%28v=vs.110%29.aspx#Y474" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.guid.op_equality%28v=vs.110%29.aspx#Y474</a></p>
    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. 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