Unnecessary DB Calls

Posts   
 
    
Kovan
User
Posts: 19
Joined: 26-Sep-2005
# Posted on: 15-Nov-2005 16:08:11   

Not sure what needs to be done to prevent the following code from going to db

        CustomerStagingEntity cust = new CustomerStagingEntity();
        CustomerAddressStagingEntity addr = new CustomerAddressStagingEntity();
        addr.Address  = "test"; 
        cust.CustomerAddressesStaging.Add(addr);

        foreach(CustomerAddressStagingEntity rr in cust.CustomerAddressesStaging)
        {
            string str = rr.Address;
        }

everything should be executed against the in memory object and not go to the database at all (in this case, the foreach is going to the database with the following query.

exec sp_executesql N'SELECT [dbo].[CMS_CustomerAddressStaging].[CustomerAddressID] AS [CustomerAddressID],[dbo].[CMS_CustomerAddressStaging].[CustomerID] AS [CustomerID],[dbo].[CMS_CustomerAddressStaging].[AddressTypeID] AS [AddressTypeID],[dbo].[CMS_CustomerAddressStaging].[Address] AS [Address],[dbo].[CMS_CustomerAddressStaging].[City] AS [City],[dbo].[CMS_CustomerAddressStaging].[State] AS [State],[dbo].[CMS_CustomerAddressStaging].[Zip] AS [Zip],[dbo].[CMS_CustomerAddressStaging].[Country] AS [Country] FROM [dbo].[CMS_CustomerAddressStaging] WHERE ( [dbo].[CMS_CustomerAddressStaging].[CustomerID] = @CustomerID1)', N'@CustomerID1 int', @CustomerID1 = 0

as you can see at the end the customerID is 0, there for this is unnecessary.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 16-Nov-2005 12:16:02   

Kovan wrote:

Not sure what needs to be done to prevent the following code from going to db


CustomerStagingEntity cust = new CustomerStagingEntity();
CustomerAddressStagingEntity addr = new CustomerAddressStagingEntity();
addr.Address  = "test"; 
cust.CustomerAddressesStaging.Add(addr);

foreach(CustomerAddressStagingEntity rr in cust.CustomerAddressesStaging)
{
    string str = rr.Address;
}

everything should be executed against the in memory object and not go to the database at all (in this case, the foreach is going to the database with the following query.

I assume youre using selfservicing. The queries you see are due to lazy loading being triggered. I'll explain below.

this line: cust.CustomerAddressesStaging.Add(addr); triggers the fetching for CustomerAddressesStaging, because the property is referenced. To avoid this from happening, simply use the other side: addr.Customer = cust;

this will also add addr to cust.CustomerAddressesStaging, without triggering lazy loading.

Frans Bouma | Lead developer LLBLGen Pro
Kovan
User
Posts: 19
Joined: 26-Sep-2005
# Posted on: 21-Nov-2005 23:31:11   

Why is IsNew not being checked on the collection instead of having to assign the parent to the child, this seems like it would be easier to do instead of having to assign the child to the parent as most of us use the .AddNew() on the collection.

|K|

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Nov-2005 06:57:41   

cust.CustomerAddressesStaging.Add(addr);

To Add addr to the CustomerAddressesStaging of the cust, the collection should be fecthed first that's why it's accessing the database, that's how lazy loading works.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 22-Nov-2005 10:57:41   

Walaa wrote:

cust.CustomerAddressesStaging.Add(addr);

To Add addr to the CustomerAddressesStaging of the cust, the collection should be fecthed first that's why it's accessing the database, that's how lazy loading works.

To elaborate on this a bit: this line: cust.CustomerAddressesStaging.Add(addr);

can be seen as: a) fetch all CustomerAddressesStaging for cust into memory and add addr b) add addr to the collection CustomerAddressesStaging in cust.

which one is it? Unclear, so the code picks one, namely a). If it would pick b) lazy loading wouldn't work. If you want not to use / trigger lazy loading (e.g. b)), use the opposite set action.

Btw, using AddNew() is for Grids which want to create a new row in the grid (as it's part of IBindingList). As in v2.0 the binding logic is moved to an entity view class, AddNew will be moved as well. It's therefore recommended that you use the proper ways to create a new instance: using normal .NET object instantiation.

Frans Bouma | Lead developer LLBLGen Pro