Using Salesforece API via .Net (C#) I try to implement a Parent - child Relation between a custom object.
Whichever is going to be detail object create a master detail field which would point to the other object. this would achieve the relationship. I want to Create a Parent as well as Child custom object at a same time ? is there any way to create a parent - child relationship between a custom object via foreign keys?
When given Object reference to its parent Object Salesforce give error status:
More than 1 field provided in an external foreign key reference in entity: ChidObj__c
Related
I have a table which is used to add couples. The table schema like below
MyTable:
Id
Name
SpouseId
My aim is to get the spouse details also from the same table using spouseId. Here i can relate the spouseId to Id of same table as self reference.
Issue:
From UI, i got object of MyTable which also includes the object of spouse which is also MyTable type.Here when EF inserts the data, 2 entries created and updated the SpouseId of child object as its parentId.
But here i also need to update SpouseId of parent object as child object id.
Any advise on this? or my design is wrong?
Your schema looks funny mate! I would suggest you change the design have a separate table which holds relationships.
Your design might cause problems while inserting as well.
This question isn't code-centric, it's a question about idioms. I'm using Backbone/Marionette on the front and C#, NHibernate in the back.
I've got a few tables mapped and working for creates and updates. For simplicity, say I've got two tables, a parent and child, and the child has potentially many rows. The child reference to the parent is not-nullable, so I've got an Inverse relationship, and that's all working. The flow of data from Backbone to the Controller to NHibernate is pretty simple, and I'm using ISession.SaveOrUpdate() at the end. I can post mappings and so forth if necessary. I will say that the mappings Fluent NHibernate generates uses a Bag on the parent.
Here's a concrete example of the situation I'm trying to understand. Say I've got a parent entry with two rows in the child table. I manipulate the data so that one of the children is removed, but no other changes are made. Javascript sends over an object "tree" with the parent entry and the one child row that's left. The mappings are all handled fine, but the sql that's generated is a bunch of (unnecessary, but whatever) update statements. What I would like to happen instead is that NHibernate notices that there is only one child relationship in this new object, but there are two children in the actual database, and then NHibernate deletes that other child. The 'cascade-delete-orphans' option is not working, because the other child isn't actually being orphaned. It still has a reference to the parent in the fk column, and that column is non-nullable anyway, which is why I used the Inverse mapping option.
Is that possible to setup in the mappings? If not, what is a good way to tackle this situation?
Since you are sending an object from the client side, and then create the entity from that object and try to persist, NHibernate will not automatically delete the child entity since it does not know the child object is deleted (it only see you are only try to update one parent entity and a child entity), which is correct in my opinion. For example if you want to just update the parent entity field, then you have to load entire object graph to do it, otherwise NHibernate will delete all children since they are not loaded.
What you should do here is to load the parent entity, and remove missing child entity(s) deleted from it and then persist (instead of mapping the entity), code should look like following,
void Update(ParentDto parentDto){
Parent parent = _session.Get<Parent>(parentDto.Id);
//update parent fields
var childRemoved = //find removed child from parent;
parent.Children.Remove(childRemoved);
_session.SaveOrUpdate(parent);
}
Is it possible to pass a reference at load time of a parent to it's children without a relationship back to the parent from the child?
I would prefer to NOT have a mapped property for this purpose (i.e. prefer not to use bi-directional relationships for this purpose).
You could probably do it with a linking table but you'd have to enforce one-to-many via code otherwise it could easily become a many-to-many. The jist of the linking table would be that it keeps the association between the parent and its children, rather than the child object itself.
I have a object structure like this:
public class Entity
{
IList<Relationship> Relationships{get;set;}
}
public class Relationship
{
public Relationship(Entity parent, IList<Entity> children)
{
//set properties
}
Entity Parent{get;private set;}
IList<Entity> Children{get;private set;}
}
The Relationship contains all information about the parent and child instances, and I would like to share the same Relationship instance on the parent and all child instances that make up the relationship.
Now when I come to load my entities from the db I start with the top entity, which then loads the relationships. I thought I could just cache the relationship I am building and reuse the same instance for the children. But this doesn't work as to create a relationship I need to load all child entities, so each child entity tries to recreate the same relationship I am currently trying to get the children for before it has been created, so I end up creating all of the relationship instances below me in the tree before I can add the relationship to the cache.
Is there a way I can get round this without making the children a settable property of my relationship, so I can create the reference to the relationship before the children are created?
So I managed this by introducing a RelationshipBuilder class which I used to keep track of the parents and children of a relationship without actually creating it.
This enabled me to load an entity and create its relationships in the builder, when the relationships tried to create the children they checked with the builder to see that the relationship was already being created and returned.
This meant I could navigate all the way down the hierarchy loading all of the entity instance and setting the parent entity and child entities associated with the relationships. Once all of the entities had been loaded and the recursion had completed and we were returned to the entry point where we started loading the entities I ask the relationship builder to resolve all the relationships. This then creates each relationship it has been informed about and sets the relationship instance on the parent and child instances which are involved in that relationship.
Is there a way to set default child entities for a new entity without having to query all of them using ObjectQueries?
Sort of. You can set the EntityKey directly. This means you will not be able to access the value of the child entity, but sometimes you don't need to do that. There is information about doing this in this post.