This is a migrated thread and some comments may be shown as answers.

Graph Update

4 Answers 55 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Garry
Top achievements
Rank 1
Garry asked on 01 Feb 2015, 02:49 PM
Hi,

I am looking at the Telerik ORM as an option for an application we are going to build which is graph heavy i.e. I need to be able to update/insert/delete graph items and submit in one transaction. I have had some success using the Telerik ORM buy have hit a stumbling block when trying to delete records.

int orderId = 6;
   Order detahcedOrder = null;
 
   using (var context1 = new MyModel())
   {
       FetchStrategy fetchStrategy = new FetchStrategy();
       fetchStrategy.LoadWith<Order>(e => e.OrderItems);
 
       List<Order> orders = context1.Orders.Where(i => i.OrderId == orderId).ToList();
       detahcedOrder = context1.CreateDetachedCopy<Order>(orders, fetchStrategy).FirstOrDefault();
   }
 
   var addMe = new OrderItem { Qty = 1, ProductId = 2 };
   detahcedOrder.OrderItems.Add(addMe);
 
   var updateMe = detahcedOrder.OrderItems.SingleOrDefault(h => h.OrderItemId == 8);
   updateMe.Qty = updateMe.Qty + 5;
 
   var removeMe = detahcedOrder.OrderItems.SingleOrDefault(h => h.OrderItemId == 9);
   detahcedOrder.OrderItems.Remove(removeMe);
 
   using (var context2 = new MyModel())
   {
       context2.AttachCopy(detahcedOrder);
       context2.SaveChanges();
   }

My app will be web based and I need to work with graphs in a detached state. The above works fine for inserts and updates, but fails when deleting records with;

Update failed: Telerik.OpenAccess.RT.sql.SQLException: Cannot insert the value NULL into column 'OrderId', table 'DemoDB.dbo.OrderItem'; column does not allow nulls. UPDATE fails.

I am clearly doing something wrong. Can anyone advise on what I should be doing?

Many thanks,

George




4 Answers, 1 is accepted

Sort by
0
Garry
Top achievements
Rank 1
answered on 01 Feb 2015, 04:09 PM
Hi,

I have done some reading on this and it appears that the framework will try and set the foreign key of the child table to null rather than delete it. I suppose the question is why? If I have removed an object from a collection and then submitted the change to a database it is because I don't want it anymore i.e. delete it. There must be workaround for this?

Thanks,

George
0
Ralph Waldenmaier
Telerik team
answered on 04 Feb 2015, 01:45 PM
Hi Stylli,
Thank you for contacting us.
I have tried to reproduce the reported behavior, though I was not able to.

Following your example. I was able to add and modify objects in the collection. Though deleting objects from the database based on missing values in the detached collection is an operation which is considered to be dangerous. Hence you have not specified a fetchgroup to expose the whole graph and are now trying to attach an object which does not have any objects of the graph. Do you want to delete the whole lot in this case? Therefore we decided to not handle delete operations on such object graphs.

Additionally, attached you can find the sample solution I used to reproduce your behavior.

I hope this information is helpful for you.
Do come back in case you need further assistance.

Regards,
Ralph Waldenmaier
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
Garry
Top achievements
Rank 1
answered on 05 Feb 2015, 08:52 PM
Hi Ralph,

Thank you for your response.I have looked at your example but it doesn't feel correct in that generally speaking referential integrity of data with within a database would be enforced. If I have understood your demo correctly, I would have to break referential integrity and mange this within my app. I have put a demo app together to demonstrate the issue but I don't appear to be able to attach it to this reply. Can I open a support ticket?

Thanks,

George
 
0
Ralph Waldenmaier
Telerik team
answered on 06 Feb 2015, 01:17 PM
Hi Stylli,
Let me summarize my answer from your support ticket.
All you need to do is specifying the OrderId column from the OrderItem table to be nullable. This will allow to set the OrderId column to null which is what Data Access is doing in your example.

Hope this helps.
Do come back in case you need further assistance.


Regards,
Ralph Waldenmaier
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
Data Access Free Edition
Asked by
Garry
Top achievements
Rank 1
Answers by
Garry
Top achievements
Rank 1
Ralph Waldenmaier
Telerik team
Share this question
or