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.