DataSource.EntityCollection and EntityView2<T>

Posts   
 
    
jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 24-Jan-2007 04:03:44   

adapter asp.net 2.0 orm 2.0.7.122

I have a webform that contains a datasource which contains an entity collection. Within a button click event not linked to the datasource I want to

  • create a filtered view of the datasource's entity collection
  • update records within this view
  • insert a new record into the db outside the datasource
  • call the datasource.refetchIf I do this the displayed data in the gridview is only the filtered record and not all records. It seems to keep the IEntityView2 filter properties even though that filtered view only exists within the button click event.

If I use the entitycollection.findmatches() function I get my index, execute my actions and refetch with out a problem. This is my current solution.

After re-reading the docs I believe I needed to use EntityView2<T> instead of the IEntityView2 so I could set the datachangeaction to NoAction. However I could not cast the datasource.entitycollection.defaultview to an EntityView2<T>. I received an error stating an invalid cast between non-generic collection and generic collection of type<t>.

Shouldn't this be possible? convert an entitycollection from a datasource object to a generic entityview? If so, what am I missing? If not, what prevents this cast?

datasource object

<llbl:LLBLGenProDataSource2 
     ID="CommissionRatesDataSource" 
     runat="server" 
     AdapterTypeName="SBI.MacolaApps.DAL.DatabaseSpecific.DataAccessAdapter,
                                     SBI.MacolaApps.DALDBSpecific"
     DataContainerType="EntityCollection"
     EntityFactoryTypeName="SBI.MacolaApps.DAL.FactoryClasses.BrokerCommissionRateEntityFactory,
                                         SBI.MacolaApps.DAL"
     LivePersistence="False" 
     OnPerformGetDbCount="CommissionRatesDataSource_PerformGetDbCount" 
     AllowDuplicates="False" 
     EnablePaging="True" 
     OnPerformSelect="CommissionRatesDataSource_PerformSelect" 
     OnPerformWork="CommissionRatesDataSource_PerformWork" />
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-Jan-2007 08:02:59   

Let's talk about the initial problem.

When you performed your work in the buttonClick event, I guess you used the DefaultView of the entityCollection. And that's the view used in the databinding.

Instead create a new EntityView:

EntityView2<CustomerEntity> YourView = new EntityView2<CustomerEntity>(YourCollection);
jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 24-Jan-2007 14:45:58   

yes, I tried using the DefaultView in my first attempts.

declaring a new EntityView2<T> throws an exception at runtime. (successfully compiles at build time)

EntityCollection<BrokerCommissionRateEntity> myCollection = (EntityCollection<BrokerCommissionRateEntity>)this.CommissionRatesDataSource.EntityCollection;
EntityView2<BrokerCommissionRateEntity> myView= new EntityView2<BrokerCommissionRateEntity>(myCollection );

Invalid Cast Exception: Unable to cast object of type 'SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionNonGeneric' to type 'SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase2`1[ SBI.MacolaApps.DAL.EntityClasses.BrokerCommissionRateEntity]'. What does the "1" represent in the error message?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-Jan-2007 15:58:42   

I think the problem is in the following line:

EntityCollection<BrokerCommissionRateEntity> myCollection = (EntityCollection<BrokerCommissionRateEntity>)this.CommissionRatesDataSource.EntityCollection;

As it seems that you are casting a nonGeneric collection to a generic one.

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 24-Jan-2007 16:15:30   

the following code works

EntityCollection<BrokerCommissionRateEntity> myCollection = new EntityCollection<BrokerCommissionRateEntity>();
foreach (IEntity2 entity in this.CommissionRatesDataSource.EntityCollection)
    myCollection.Add((BrokerCommissionRateEntity)entity);
EntityView2<BrokerCommissionRateEntity> myView = new EntityView2<BrokerCommissionRateEntity>(myCollection, filter,null, PostCollectionChangeAction.NoAction);

but this seems like a hack. Could this cast be concidered for a future version. I know 2.1 is locked for new feature, maybe 2.2 or 3.0.

thanks again for the insight on DefaultView

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 24-Jan-2007 16:52:36   

The PostCollectionChangeAction value isn't settable when the view is created already. The DefaultView (through the interface IEntityCollection2 for example) is already created, as the grid binds to it.

I can easily expose this property through the interface and the class, so you could do: PostCollectionChangeAction save = datasource.EntityCollection.DefaultView.PostCollectionChangeActionValue; datasource.EntityCollection.DefaultView.PostCollectionChangeActionValue = PostCollectionChangeAction.NoAction; //.. do filtering etc. here

// done, reset to value before routine datasource.EntityCollection.DefaultView.PostCollectionChangeActionValue = save;

Frans Bouma | Lead developer LLBLGen Pro
jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 24-Jan-2007 16:54:21   

that would be very helpful, thank you.