Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to SQL update not working using Repository pattern
    primarykey
    data
    text
    <p>I am using asp.net mvc for an application. I've taken some guidance from Rob Conery's series on the MVC storefront. I am using a very similar data access pattern to the one that he used in the storefront.</p> <p>However, I have added a small difference to the pattern. Each class I have created in my model has a property called IsNew. The intention on this is to allow me to specify whether I should be inserting or updating in the database.</p> <p>Here's some code:</p> <p>In my controller:</p> <pre><code>OrderService orderService = new OrderService(); Order dbOrder = orderService.GetOrder(ID); if (ModelState.IsValid) { dbOrder.SomeField1 = "Whatever1"; dbOrder.SomeField2 = "Whatever2"; dbOrder.DateModified = DateTime.Now; dbOrder.IsNew = false; orderService.SaveOrder(dbOrder); } </code></pre> <p>And then in the SQLOrderRepository:</p> <pre><code>public void SaveOrder(Order order) { ORDER dbOrder = new ORDER(); dbOrder.O_ID = order.ID; dbOrder.O_SomeField1 = order.SomeField1; dbOrder.O_SomeField2 = order.SomeField2; dbOrder.O_DateCreated = order.DateCreated; dbOrder.O_DateModified = order.DateModified; if (order.IsNew) db.ORDERs.InsertOnSubmit(dbOrder); db.SubmitChanges(); } </code></pre> <p>If I change the controller code so that the dbOrder.IsNew = true; then the code works, and the values are inserted correctly.</p> <p>However, if I set the dbOrder.IsNew = false; then nothing happens...there are no errors - it just doesn't update the order.</p> <p>I am using DebuggerWriter here: <a href="http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11" rel="nofollow noreferrer">http://www.u2u.info/Blogs/Kris/Lists/Posts/Post.aspx?ID=11</a> to trace the SQL that is being generated, and as expected, when the IsNew value is true, the Insert SQL is generated and executed properly. However, when IsNew is set to false, there appears to be no SQL generated, so nothing is executed.</p> <p>I've verified that the issue here (<a href="https://stackoverflow.com/questions/206532/linq-not-updating-on-submitchanges">LINQ not updating on .SubmitChanges()</a>) is not the problem.</p> <p>Any help is appreciated.</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.
 

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