I have two tables without any cascade deleting, the two tables have 1 to many relation ship.
When loading the entities that map that tables , and try to delete the parent entity then 2 scenarios will execute:
The entity framework will force the cascade delete when both entities were loaded.
If the child entity was not loaded it will raise an exception
How to guarantee that the entity framework will execute the second scenario always????
What I mean, I need to raise an exception without force delete cascade behavior.
thank you so much.
Related
I have a one to many relationship between two core data entities.
As Below A->>B
A ->> B : Delete Deny (i.e Deny the deletion of A while their are objects of B)
B -> A : Nullify
In multiple context, one context is unaware of other changes, so in that case i get a validation error while saving the parent context, because in one context a child is inserted to a parent which am deleting in another context.
Their is little to no information provided on how to handle validation error, most of the examples just show error to user and discard the changes. If i need to undo delete of that particular parent object can i?
Is there any option how i can handle the validation error.
NSValidationRelationshipDeniedDeleteError
I didn't find a way to undo the delete for only a certain object without losing changes. So if you have deleted a parent Entity in a childContext, then saved the childContext, the deleted entity is automatically added to parentContext.deletedObjects. I tried different approaches but the only way I was able to cancel the delete operation from the parentContext is to call parentContext.refreshObject(deletedParent, mergeChanges: false). (for sure you can try to rollback or reset the context)
It really depends on what you are going to do (so the business has a great influence here!). But I think you should work optimistically: https://en.wikipedia.org/wiki/Optimistic_concurrency_control
Sometimes, in case of conflicts, it's ok to rollback and restart the transactions.
Usually, the childContext overrides the changes of the parentContext(the child has higher priority). So the architecture of you app should work the same way. When you delete an entity in the childContext, you should also delete it in the parentContext, even if the deny rule stops you because you have inserted a new child entity. What you should do is you should remove the inserted entities then delete the parent entity. Otherwise you should restudy the architecture of the app.
I am using coredata to save the server data through web services in my application and I am storing relationships as an object to the entity.
I have many entities e.g "Inspirations" and "Products" and both are related to each other. I have a problem whenever the records are updated in the third entity which is "Filters" then the relations of the entities broke and I cannot apply filters on the entities.
[object addRelatedInspirationsObject:related];
This is how I save relationships. I am not able to figure out why the relations are being broken once the entity is updated which has no direct link with the entity.
One thing more if I fetch and save the data of any one of the entities like "Inspirations" then all the relations start to work again.
Your code should work. Here are 2 things you need to check:
Make sure related is not nil when you call your method.
Make sure you call save on a valid managed object context.
From your question it seems that entities have 1 to many relationship between them. And by the code you supplied, every things should work fine. Just make sure, you are using the Filter object from the relationship like object.filter (or obj1.obj2.filter), not accessing it via a direct NSPredicate on Filter entity and updating it. And if you are using FRC, you might also need to generate a fault against the parent entities, to get your UI updates.
I found a question very similar to mine here, but it was un-replied to and unanswered so I will try again.
I may be missing something in regards to cascade deletes and core data. I am used to cascade deletes working as designed in RDBMS apps but having issues with core data.
I have an object record that get's inserted into entity via an "add form" called modally from a table view. No problem.
In another session I will insert objects into a related details entity (many) where there is a common loadID attribute in both. No problem.
In another session, I will call up the original table view to view the parent "loads", and swipe-delete, save the context, and the parent load gets deleted from the one side entity. No problem, (except apparently details objects in a many side entity do not get removed)
When I open the sqlite data source with a db manager, I see the child records (pay detail items) are being orphaned. I have double and triple checked the cascade delete settings in the relationships. Have tried different entity relationship settings combinations, but nothing seems to get the many records to automatically delete along with the parent record.
If you can't define corresponding keys in core data, how does core data know what belongs to what when you operate in multiple sessions (contexts) adding child objects to the many-side entity?
I don't know if I'm doing something wrong or missing a vital step prior to inserting new child objects in the many table, or doing something wrong or missing a step when deleting the parent object.
But I would suggest all with cascade deletes set to open your data file and be sure there are not orphaned objects (records)
I have an entity (Org) in my Entity Framework that has a foreign key with a table that is in another database (BusinessUnit). We need the foreign key to get the description of the BusinessUnit linked with the Org. In the past (old project without Entity Framework) we were using a stored procedure to return all the information about this entity, including the BusinessUnit description, using a join. So now my problem is how to display the same information than before using Entity Framework.
I've tried, once I load my Org entity from database, to make a loop accessing to BusinessUnit to get the description for each Org, but this is too slow. My other idea was use a store procedure, but I need an extra field on my entity and Entity Frameworks gives me an 3004 error: No mapping specified for my property. I was thinking to use a complex type, but I'm not sure if it's what I need keeping in mind I have to add just a field to my entity. In that case, could I use the complex type just for "select" operations and keep my original entity for the rest of CRUD operations?
How should I proceed?
Thanks.
EF is not able to execute queries across multiple databases. If you need to perform such query you must either use database view and map the view as a new entity (it will be readonly - making it updatable requires mapping insert, update and delete stored procedures) or divide your data querying into two separate parts to load data from both databases. Divided querying can either use two contexts or you can get data from the second database using stored procedure.
The reason why you got the error is that you added the property in EDMX. EDMX can contain only properties mapped to your first database. EDMX generates entity code as partial classes If you need property manually populated from the second database you have to create your partial part (partial class) for entity and add the property in code.
I have a one-to-many relationship defined for two entities, let's just say Customer and Order. The relationship states that a Customer object can have many Order objects, but that an Order must have one Customer...a pretty standard 1:M relationship.
I have no cascading rules configured on the database and OnDeletes are set to None in my entity data model. This means deleting a customer that has orders will not be allowed.
My problem is that when the user in my application deletes a customer (that may have orders), I want to catch the exception that is thrown by enforcing the foreign key constraints. For one specific customer that has 33 orders, I get InvalidOperationException but for another customer that only has 2 orders, I get UpdateException. EF is letting the second delete statement go to the database and the database is returning the error that EF makes the UpdateException for. In the first scenario, EF doesn't even let the delete statement go to the database, it just says this is going to be invalid, because this entity instance of Customer contains Orders.
Can anybody explain why this behavior is happening?
There can be two different exceptions because there are two layers where this check is applied. First if you mark an object as deleted and execute SaveChanges EF will check its internal storage where all loaded objects are tracked. If EF finds that any tracked order related to the deleted customer is also not marked as deleted it will fire an exception (probably InvalidOperationException). If there is no tracked related order EF will execute delete operation in the database where FK relation triggers an error. This error is captured by EF and an exception is throwm (probably UpdateException with inner database related exception).