Lets say I have:
A username entity, with a Int64 as the ID and a few other fields.
A message entity, with a Int64 as the ID and a few other fields linked to the username entity via a 1 username can have 0 to many messages relationship.
I have two repositories (basic all, add, delete, save methods) for username and message entities.
But I want to associate a message entity that I generate in code (new Message() etc) with a username how would I do this with a repository and across two different objectcontexts?
I got it working via not using a repository via:
usernameEntity.Messages.Add(msg);
Anyone got any hints, rather confused as if I could split this type of logic into two different repositories.
You will simply call:
Your repositories should share a single context for one unit of work (a business transaction). You should have some method (Save, Commit or whatever else) which will save changes made on entities loaded from all repositories. This is usually handled by a separate class implementing the unit of work pattern which wrapps context and is shared among repositories. Saving changes is called on unit of work instead of repository. There is plenty of related questions to this problem.
In case of real foreign key relationship modeled in EFv4 you can also use simply: