tags:

views:

131

answers:

1

Do I need to save newly created crm entity instances before I can set relations to other crm entity instances?

I'm facing the problem that after calling CrmDataContext.SaveChanges() the newly created entities are written to the database, but the relations between those newly created instances are missing in the database.

What do I miss? Do I have to call CrmDataContext.SaveChanges() each time I create a new crm entity instance that I want to have relations to other crm entity instances?

Thanks

+1  A: 

You should be able to set relationships to other entities in a 1:N relationship before saving this entity(i.e. a lookup).

If you are wanting your entity to be referenced by another entity it will need to be Saved first OR you need to set a Guid for the entity first. Otherwise your link won't stick.

When you new up an entity its id is not set until it is saved to the database, unless you set it manually. If you set it manually it will respect the new Guid you have given it and the relationship will survive the saving process.

If you try to save an entity individually, you need to make sure that you have saved all the entities that it refers to before you save that entity, or it won't have a link.

Mark Kovalcson