IEntityCollection2.AddRange exception

Posts   
 
    
lleo
User
Posts: 4
Joined: 13-Dec-2006
# Posted on: 20-Oct-2009 11:07:43   

Hi. I'm migrating an application from - LLBLGenPro 1.0.2005.1 release - .NET Framework 1.1 - SD.LLBLGen.Pro.ORMSupportClasses.NET11.dll release 1.0.20051.60112 to - LLBLGenPro 2.6 release - .NET Framework 2.0 - SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll release 2.6.8.1211

The following code raises an Exception:


IEntityCollection2 contenuti = GetCurrentElementiCapitolato();
            
EntityCollection contenutiToAdd = new EntityCollection(new ContenutoCapitolatoEntityFactory());

using(frmSelezionaElementiCapitolati frm = frmSelezionaElementiCapitolati.NewInstance(this))
{
    frm.VisualizzaDialogForm(ref contenutiToAdd);
}

contenuti.AddRange(contenutiToAdd as IEntityCollection2);

When the last line of code is executed, an Exception is raised. The stack trace is the following:

SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase2`1.SD.LLBLGen.Pro.ORMSupportClasses.IEntityCollection2.AddRange(IEntityCollection2 c) in Stain.ASO.STAINPlus.forms.capitolati.frmCapitolato.AggiungiElementiChimici() in C:\usr\AsoSiderurgica\STAIN+\winsup_dev\Stain.ASO.STAINPlus\forms\capitolati\frmCapitolato.cs:riga 1743

and the Exception message is the following:

"c isn't of the right type"

Just to explain the code: - GetCurrentElementiCapitolato() is a private method that returns a reference to an EntityCollection<ContenutoCapitolatoEntity> - _frmSelezionaElementiCapitolati _is a form that allows the user to select a list of ContenutoCapitolatoEntity entities - if I inspect the content of _contenutiToAdd _jus before the last line of code is executed, I can see that it is correctly populated

In the 1.0.2005.1 code version, the variable contenuti was of EntityCollection type.

If I change the code like this


IEntityCollection2 contenuti = GetCurrentElementiCapitolato();
            
EntityCollection contenutiToAdd = new EntityCollection(new ContenutoCapitolatoEntityFactory());

using(frmSelezionaElementiCapitolati frm = frmSelezionaElementiCapitolati.NewInstance(this))
{
    frm.VisualizzaDialogForm(ref contenutiToAdd);
}

foreach (ContenutoCapitolatoEntity contenuto in contenutiToAdd)
{
       contenuti.Add(contenuto);
}

it works correctly.

I can't understand what I'm doing wrong.

Thanks a lot for any help.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 20-Oct-2009 11:35:43   

What is the return type of GetCurrentElementiCapitolato()? please post its signature.

contenuti.AddRange(contenutiToAdd as IEntityCollection2);

Try not to cast to IEntityCollection2 in the above line of code.

lleo
User
Posts: 4
Joined: 13-Dec-2006
# Posted on: 20-Oct-2009 13:41:21   

That is the GetCurrentElementiCapitolato() code:

private IEntityCollection2 GetCurrentElementiCapitolato()
{
     IEntityCollection2 contenuti = fgElencoElementi.DataSource as IEntityCollection2;
     return contenuti;
}

fgElencoElementi is a grid component that is placed on a Windows form. It is binded to a EntityCollection<ContenutoCapitolatoEntity> collection, so its DataSoruce property references an EntityCollection<ContenutoCapitolatoEntity> object.

Try not to cast to IEntityCollection2 in the above line of code.

I tried it but the exception is thrown anyway (EntityCollection implements IEntityCollection2).

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Oct-2009 05:54:24   

This is the cause and workaround about this: http://llblgen.com/tinyforum/Messages.aspx?ThreadID=11607&StartAtMessage=0&#76935 That thread is applied to DataSource but it the same concept/workaround you must apply.

David Elizondo | LLBLGen Support Team