We are working using the Unit Of Work pattern combined with the Generic Repository pattern.
Are POCO’s are built like this:
public class Aclass: IEntity{
public int ID;
public virtual Bclass Bproperty;
public virtual Cclass Cproperty;
}
With IEntity
public interface IEntity{
int ID;
}
When we are creating an unique A-object, we are referring to an already existing B-object and C-object.
When we .SaveChanges() A is saved, but also a copy of B and C. So instead of referring to the original B and C objects, A’s Bproperty and Cproperty are referring to new objects.
We can fix it by attaching, but given the fact we are working with patterns, this screws with our level of abstraction.
Does anyone have a clue on how to fix this in a clean way?
You have to mark the existing B and C objects as
Unchangedbefore theSaveChangesor
(depending on what kind of context you’re using)