rarandas wrote:
Hello,
I have a big problem using a project generated by llblgen, using adapter with C#.
Description:
When i use an entitycollection with severals entity and not persist this entity collection but save other entity to database,
the entity collection is persisted as well.
For example:
//I have an entity Order
OrderEntity Order = new OrderEntity();
Order.Date = DateTime.Now;
Order.CustumerId = 7;
Order.Price = 800.00;
//The order has a entitycollection of ItemOrderEntity and it´s empty
Order.ItemOrder.Clear();
//I have another not related entityCollection of ItemOrderEntity
EntityColletion Items = new EntityCollection(ItemOrderEntityFactory)
for(int i = 1; i< 10 ; i++)
{
ItemOrder Item = new ItemOrder();
Item.AnyField = i;
Itens.Add(Item);
}
//The code above could be with a arraylist
ArrayList Items = new ArrayList();
for(int i = 1; i< 10 ; i++)
{
ItemOrder Item = new ItemOrder();
Item.AnyField = i;
Itens.Add(Item);
}
//So, The code to Save Order
DataAccessAdapter adp = new DataAccessAdapter("ConnectionString");
adp.SaveEntity(Order)
When i use this code above, i´m trying to save just the OrderEntity Order, note that Order.ItemOrder is empty because the
code Order.ItemOrder.Clear(). But when i save Order, is saved also many ItemOrder that was added in entitycollection or
arraylist.
By this example i noted that llblgen cache the actions to persist to database late. What i have to do to save just what i
want intead everything wich a i have done in memory?
Thank´s
Rubens Arandas
Hi, there. The framework supports, by default, "recursive saves" which will persist all related entities to the "root" entity. You can turn this off by calling the overload of adapter.SaveEntity() that has 4 arguments, and pass "False" in for "recurse". This will only save the base entity and ignore related entities, collections, and the like.
However, entities are not added to a given graph unless specifically assigned to by setting the various entities' object variables:
myOrder.OrderItem.Add(myItem)
//or
myItem.Order = myOrder
Somewhere in your code you're making an assignment that resembles one of these two options. Either remove that assignment, or call the overload of .SaveEntity() that lets you specify "False" for recurse.
Jeff...