EntityBase2.OnBeforeEntitySave - ChangesNotSaved

Posts   
 
    
Posts: 98
Joined: 09-Feb-2005
# Posted on: 16-Mar-2007 19:24:05   

I have an entity X, that prior to being saved, needs to create/update/remove some related entities. So I've overridden OnBeforeEntitySave in X (not DataAccessAdapter). When I save a single X, this code is triggered, the related entities are manipulated, and the save is successful.

However, when I save an EntityCollection<X>, the call is still made, but the related entites are ignored.

I have verified that the appropriate entities are indeed updated in the object model (they're there before the method exits, and are still there after the save completes.) However, the sql to insert them is never fired. I can modify fields that are directly part of X, and have the updates take, just not the related entities.

Is there a different sequence of events that occurs for saving entity collections? Is there a way to get this to work in another way?

Thanks.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 17-Mar-2007 02:02:25   

I believe the default behavior for saving collections will not recurse through the related entities, but the default behavior for one entity will. You may need to set this to happen when you save a collection.

Posts: 98
Joined: 09-Feb-2005
# Posted on: 17-Mar-2007 02:30:22   

Hi BClubb, thanks for the reply, but I have the recuse variable set to to true. If I add the values, and add them before calling DataAccessAdapter.SaveCollection, rather than in the override, they are saved to the database w/ no issue. The problem is of course that I don't want to be forced to remember to execute this everywhere. Any ideas? Would it work better if I overrode the DataAccessAdapter instead?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Mar-2007 01:47:48   

Hi Jeff, Would you post your code, so we can get insights of your scenario?

David Elizondo | LLBLGen Support Team
Posts: 98
Joined: 09-Feb-2005
# Posted on: 18-Mar-2007 18:38:43   

EDIT... The attachment upload failed for what I assume are size-related issues.

Posts: 98
Joined: 09-Feb-2005
# Posted on: 18-Mar-2007 18:39:52   

Where can I post an attachment more than 256kb? I have a demo app w/ datbase, console, and llbl generated code that demonstrates the problem succinctly.

Posts: 98
Joined: 09-Feb-2005
# Posted on: 18-Mar-2007 18:43:30   

Ok, well because I don't want to wait too long for a response, here are the 2 critical file code snippets:


/// Just sets up some dummy entities to save
EntityCollection<TestEntity> tests = new EntityCollection<TestEntity>();
TestEntity test0 = new TestEntity();
test0.Name = "Hello";
TestEntity test1 = new TestEntity();
test1.Name = "World";

tests.Add(test0);
tests.Add(test1);

//////////////////////////////////////////////////////////////////////////////////////////
/// Calling AddHash here creates the objects, and DOES save them
/////////////////////////////////////////////////////////////////////////////////////////////
//test0.AddHash();
//test1.AddHash();

/// Executes the save
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.SaveEntityCollection(tests, false, true);
}


public partial class TestEntity
{
    protected override void OnBeforeEntitySave()
    {
        //////////////////////////////////////////////////////////////////////////////////////////
        /// Calling AddHash here creates the objects, but does NOT save them
        //////////////////////////////////////////////////////////////////////////////////////////
        /// AddHash();
        base.OnBeforeEntitySave();
    }

    public void AddHash()
    {
        // Left so that you much comment out AddHash() above to get things to work
        this.TestHashCollection.Clear();

        TestHashEntity hash = new TestHashEntity();
        hash.NameHash = this.Name.GetHashCode().ToString();
        this.TestHashCollection.Add(hash);
    }
}

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Mar-2007 19:32:32   

I could move your thread to bugs/issues or to the helpdesk forum which allow larger attachment sizes

Frans Bouma | Lead developer LLBLGen Pro
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Mar-2007 20:28:40   

Thaks for post your code. OnBeforSaveEntity is for last minute changes (Ref. LLBLGenPro Help). Pleae review the follow workaround for your question: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=8412

Hope helpful.

David Elizondo | LLBLGen Support Team