Changing related entity instances

Posts   
 
    
jmcervera
User
Posts: 27
Joined: 03-Jun-2004
# Posted on: 20-May-2005 11:44:05   

Hello, Although I have LLBLBEN for some time, I couldn´t find time to begin working with it, and now I am beginning with my first project. And as a newcomer I have some problems. simple_smile My question is: For example I get an order entity with its related customer entity. Now, I select another customer and want to charge the order.Customer to the new customer selected.


 DataAccessAdapter adapter = new DataAccessAdapter(true);
 OrderEntity order = new ArticuloEntity(pkey);
 adapter.FetchEntity(order);
 order.Customer = 
  (CustomerEntity) adapter.FetchNewEntity(new CustomerEntityFactory(),   
          order.GetRelationInfoCustomer() );
 adapter.CloseConnection();
            
adapter.FechEntity(newCustomer);
order.Customer = newCustomer;

When I execute the last sentence I have an error like this

System.NullReferenceException : Object reference not set to an instance of an object.

in as DesetupSyncCustomer method.

How should I make something like this ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 20-May-2005 12:25:34   

I think this line is not completely correct:


 OrderEntity order = new ArticuloEntity(pkey);

Shouldn't that be:


 OrderEntity order = new OrderEntity(pkey);

?

When I try to reproduce what you're doing, it works ok, so I can't reproduce it:


[Test]
public void DereferenceTest()
{
    using(DataAccessAdapter adapter = new DataAccessAdapter())
    {
        OrderEntity o = new OrderEntity(10254);
        adapter.FetchEntity(o);
        o.Customer = (CustomerEntity)adapter.FetchNewEntity(new CustomerEntityFactory(), o.GetRelationInfoCustomer());

        CustomerEntity c = new CustomerEntity("BLONP");
        adapter.FetchEntity(c);
        o.Customer = c;
    }
}

Frans Bouma | Lead developer LLBLGen Pro
jmcervera
User
Posts: 27
Joined: 03-Jun-2004
# Posted on: 20-May-2005 13:35:45   

Otis wrote:

I think this line is not completely correct:


 OrderEntity order = new ArticuloEntity(pkey);

Shouldn't that be:


 OrderEntity order = new OrderEntity(pkey);

?

Yes it should be. It was a mistake I made translating the name entity to a new one that I thought that better explain the relation of the objects.

But with the same test you have show me, translating the entity names. I have the same error


[Test]
public void DereferenceTest()
{
    using(DataAccessAdapter adapter = new DataAccessAdapter())
    {
        ArticuloEntity o = new ArticuloEntity(5);
        adapter.FetchEntity(o);
        o.Formato = (FormatoArticuloEntity)adapter.FetchNewEntity(new FormatoArticuloEntityFactory(), o.GetRelationInfoFormato());
        FormatoArticuloEntity c = new FormatoArticuloEntity(2);
        adapter.FetchEntity(c);
        o.Formato = c;
    }
}

Between Articulo and FormatoArticulo there is a (M:1) relation

The error is

ArticulosPublicitariosTest.Class1.DereferenceTest : System.NullReferenceException : Object reference not set to an instance of an object.

and the stack

at ArticulosPublicitarios.DAL.EntityClasses.ArticuloEntity.DesetupSyncFormato(Boolean signalRelatedEntity) in C:\PROYECTOS\ArticulosPublicitarios\DAL\DatabaseGeneric\EntityClasses\ArticuloEntity.cs:line 863

at ArticulosPublicitarios.DAL.EntityClasses.ArticuloEntity.SetupSyncFormato(IEntity2 relatedEntity) in C:\PROYECTOS\ArticulosPublicitarios\DAL\DatabaseGeneric\EntityClasses\ArticuloEntity.cs:line 876 at ArticulosPublicitarios.DAL.EntityClasses.ArticuloEntity.SetRelatedEntity(IEntity2 relatedEntity, String fieldName) in C:\PROYECTOS\ArticulosPublicitarios\DAL\DatabaseGeneric\EntityClasses\ArticuloEntity.cs:line 300 at SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase2.Add(IEntity2 entityToAdd) at ArticulosPublicitarios.DAL.EntityClasses.FormatoArticuloEntity.SetRelatedEntity(IEntity2 relatedEntity, String fieldName) in C:\PROYECTOS\ArticulosPublicitarios\DAL\DatabaseGeneric\EntityClasses\FormatoArticuloEntity.cs:line 232 at ArticulosPublicitarios.DAL.EntityClasses.ArticuloEntity.set_Formato(FormatoArticuloEntity value) in C:\PROYECTOS\ArticulosPublicitarios\DAL\DatabaseGeneric\EntityClasses\ArticuloEntity.cs:line 1483 at ArticulosPublicitariosTest.Class1.DereferenceTest() in c:\proyectos\articulospublicitarios\articulospublicitariostest\class1.cs:line 54

¿Maybe is it something I have bad designed in the llblgen project file?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 20-May-2005 14:17:43   

OK, what's the version of llblgen pro you're using? (date in about box + version, as in: 1.0.2004.2 May 18th or so).

If you set a breakpoint in ArticuloEntity.DesetupSyncFormato, where exactly does it crash and what is null?

Frans Bouma | Lead developer LLBLGen Pro
jmcervera
User
Posts: 27
Joined: 03-Jun-2004
# Posted on: 20-May-2005 15:20:53   

Otis wrote:

OK, what's the version of llblgen pro you're using? (date in about box + version, as in: 1.0.2004.2 May 18th or so).

If you set a breakpoint in ArticuloEntity.DesetupSyncFormato, where exactly does it crash and what is null?

The version is: Version 1.0.2004.2 May 1st, 2005


private void DesetupSyncFormato(bool signalRelatedEntity)
{
    if(_formato != null)
    {
                
        // disconnect the entity from this entity
        _formato.AfterSave-=new EventHandler(OnEntityAfterSave);
        if(signalRelatedEntity)
        {
            _formato.UnsetRelatedEntity(this, "Articulo");
        }
        base.UnsetEntitySyncInformation("Formato", _formato,   ArticuloEntity.Relations.FormatoArticuloEntityUsingFormatoArticuloId);
                
        _formato.DescripcionChanged-=new EventHandler(OnDescripcionFormatoChanged);
        _formato = null;
        SetNewFieldValue((int)ArticuloFieldIndex.FormatoArticuloId, null);
    }
}

The error appears at execution of the de-registration of the event handler


     _formato.DescripcionChanged-=new EventHandler(OnDescripcionFormatoChanged);

The indefined value is **_formato**

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 20-May-2005 18:12:34   

That line is a field mapped onto a related field. I don't have that in my test entities.

I'll try to fix this. It's a bug in the templates.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 20-May-2005 19:02:19   

A fix for this is now available. Either download the templates archive from the templates section in the customer area, or download the full installer again. simple_smile You've to regenerate your code after the template installation of course.

Frans Bouma | Lead developer LLBLGen Pro
jmcervera
User
Posts: 27
Joined: 03-Jun-2004
# Posted on: 20-May-2005 19:44:57   

Frans, Thank you very much. You are an incredible man, I could never thought in a better support service than the one you give. stuck_out_tongue_winking_eye