Related collections are not shown when using in-memory persistor

Dec 9, 2011 at 2:32 PM

I have a fairly simple fixture and using the in-memory persistor it loads the data nicely. There's a one on many relation between "Owner" and "Property". Im able to see the Owner on the Property enity but I don't see the Properties on the Owner. Here's the fixture:

public void Install()
{
    Owner owner = CreateNewOwner("OW1", "Owner 1");
    Property property = CreateNewProperty("PRP", "Property", owner);
}

private Owner CreateNewOwner(string code, string name)
{
    Owner obj = Container.NewTransientInstance<Owner>();
    obj.Code = code;
    obj.Name = name;
    Container.Persist(ref obj);
    return obj;
}

private Property CreateNewProperty(string code, string name, Owner owner)
{
    Property obj = Container.NewTransientInstance<Property>();
    obj.Code = code;
    obj.Name = name;
    obj.Owner = owner;
    Container.Persist(ref obj);
    return obj;
}

Coordinator
Dec 9, 2011 at 2:48 PM

Ah, yes, sorry  -  this is not documented. 

When working with the InMemory persistor  -  which in fact pre-dates the EntityPersistor by several years  -  reverse associations are not made automatically.  You have to set them up.  So you'll need to explicitly say in the last method:

obj.Properties.Add(owner)

But what about outside fixtures?  Well, if you are likely to be switching between the InMemory and Entity persistors, then the safest bet is to always make the reverse associations explicilty. EF will just ignore it (they won't be doubled up).  And the safest way to do that is to always add things through an explicit AddTo method.

FYI, this was how we always built Naked Object domain models in the past  -  even to work with other persistors.  EF provided a positive surprise for us in managing the reverse associations automatically, so we've tended to get out of that habit  - which does impact the ability to switch between the persistors.

Dec 9, 2011 at 2:58 PM

Makes sense. Thanks!

-Jeroen