Question

Using Telerik OpenAccess ORM I have 2 objects User and Investment. More specifically Investments contains a foreign key to User as any typical one to many relationship. In other words each User can have mutliple Investments but each Investment can only have one user.

I have then attempted to utilize the open access feature 'Is Managed'

enter image description here

Which should mean that I can do something like User.Investments.Clear(); and it deletes all the related investments (or at least this works fine in many-to-many relationships) but unfortunately when I attempt this I am greeted with the following error.

"Update failed: Telerik.OpenAccess.RT.sql.SQLException: Cannot insert the value NULL into column 'UserID', table 'CODECorp.dbo.Investment'; column does not allow nulls. UPDATE fails."

Clearly what the ORM is trying to do is remove the association (i.e. foreign key) from the investment object to the user rather than deleting it. I have confirmed this by running SQL profiler and can see that it's running an Update rather than a Delete.

So what am I missing here? Why is it incorrectly trying to remove the association rather than simply deleting the row as you would expect?

Était-ce utile?

La solution

By design, the behaviour of the navigation properties with IsManaged set to True, in scenarios where a child object is deleted from the collection of the parent object, is to remove the relationship between the two objects. In other words, Telerik Data Access (previously known as Telerik OpenAccess ORM) will keep the child record in the database but will generate a statement that attempts to set the foreign key to NULL.

A solution in this situation would be to pass the collection to the Delete method of the context. For example:

dbContext.Delete(User.Investments);
dbContext.SaveChanges();

This will produce the necessary DELETE statement. More details about the management of navigation properties with Telerik Data Access is available in this documentation article.

I hope you find this feasible. I am looking forward to your feedback.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top