collection is read-only

Posts   
 
    
alexdresko
User
Posts: 336
Joined: 08-Jun-2004
# Posted on: 05-Jul-2005 03:20:25   

Hi Frans,

Why doesn't this block of code work? Note GPAgencyUsers is a table that links multiple users to multiple agencies.



GP.DAL.EntityClasses.UsersEntity user = GP.DAL.EntityClasses.UserEntity(userID);

_adapter.FetchEntity(user);

GP.DAL.EntityClasses.GpAgencyEntity agency = new GP.DAL.EntityClasses.GpAgencyEntity(agencyID);

_adapter.FetchEntity(agency);

bool success = false; 

if (agency.Fields.State == SD.LLBLGen.Pro.ORMSupportClasses.EntityState.Fetched &&
    user.Fields.State == SD.LLBLGen.Pro.ORMSupportClasses.EntityState.Fetched)
{
    // if I don't do this, it says the collection is read-only. 
    // it still doesn't allow me to update the collection though. 
    agency.UsersCollectionViaGpAgencyUsers.IsReadOnly = false;
    

    agency.UsersCollectionViaGpAgencyUsers.Add(user);

    // This is what I tried first.. didn't update the GPAgencyUsers table
    //success = _adapter.SaveEntity(agency, false, null, true);
    
    // I think this would work, but it doesn't update the GPAgencyUser table either. 
    // success is always false
    if (_adapter.SaveEntityCollection(agency.UsersCollectionViaGpAgencyUsers) > 0)
    {
        success = true;
    }

    

}


return success;


I feel like I'm doing something stupid. simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 05-Jul-2005 07:58:52   

collections mapped on m:n relations are readonly, because adding an entity to these collections means an intermediate entity also should be added, though that COULD mean the intermediate entity requires fields to be set (because it has non-PK fields for example). To keep code consistent, you always have to work via the intermediate entity.

So Order - OrderDetails - Product, where Order has a collection 'Products' which is an m:n relation, you should add an OrderDetails object to Order.OrderDetails and to the Product.OrderDetails collection. That would link them together. The Order.Products m:n collection isn't updated with that action though.

Frans Bouma | Lead developer LLBLGen Pro