No auto-save for m:n relation mapping?

Posts   
 
    
benles
User
Posts: 62
Joined: 02-May-2005
# Posted on: 31-May-2006 00:40:47   

It looks like SaveMulti() will not get called on an m:n related field when Save() is called

a = new FooEntity() b = new BarEntity()

a.Value = "test"

b.Foos.Add(a)

b.Save() ' this will not save A

b.Foos.SaveMulti() ' it seems that this separate call is needed

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 31-May-2006 03:25:38   

b.Save() will not recursively save dirty entities. You would have to use b.Save(true). Does this result in the behavior that you need?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 31-May-2006 10:04:02   

m:n relations are read-only as they're a view on the state of 3 entities. So they're not saved, as their state isn't maintained in the entities in-memory.

The problem with m:n relations in-memory is that there's a 3rd entity involved, the intermediate entity. That entity doesnt have to be loaded per-se, which means you can't save an m:n relation without the intermediate entities.

so instead, use the a 1:n b m:1 c chain to save an m:n relation: instead of saving department.Employees, save: department departmentemployee employee where department has a collection of departmentemployee entities which reference the employee entities. Save it recursively.

Frans Bouma | Lead developer LLBLGen Pro
benles
User
Posts: 62
Joined: 02-May-2005
# Posted on: 01-Jun-2006 23:17:00   

Otis wrote:

The problem with m:n relations in-memory is that there's a 3rd entity involved, the intermediate entity. That entity doesnt have to be loaded per-se, which means you can't save an m:n relation without the intermediate entities.

I tried an alternative approach with 1:m foreign keys as follows:

Person (table) Name FkAddressId

Address (table) Street FkCountryId

Country (table) Name

dim p as PersonEntity = new PersonEntity

p.Name = "Bob" p.Address.Street = "123 Lane" p.Address.Country = "USA"

p.Save(true)

Address and Country are not saved though unless I explicitly assign a new AddressEntity and CountryEntity. Why is that? It seems like this should result in a recursive save even if the entities are new.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 02-Jun-2006 07:50:52   

You should use new to instantiate teh objects.

And take care, p.Address.Country is an object not a string property. this too needs to be instantiated then you may use the following to set the name p.Address.Country.Name = "USA"

benles
User
Posts: 62
Joined: 02-May-2005
# Posted on: 03-Jun-2006 13:21:38   

Walaa wrote:

You should use new to instantiate teh objects.

And take care, p.Address.Country is an object not a string property. this too needs to be instantiated then you may use the following to set the name p.Address.Country.Name = "USA"

Right, I meant Country.Name.

Thanks!