Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Problem to use AsNoTracking query in master-detail entities.

$
0
0

Hi

I have 2  tables in master-details scenario as follow :

DefaultVisitProductHeaders -> Master table
DefaultVisitProductDetails -> Detail table

My problem is that when i load details data with AsNoTracking as follow :

List<DefaultVisitProductDetails> packageDetails = _dbContext.DefaultVisitProductDetails.AsNoTracking().Where(d => d.DefaultVisitProductHeaderRowID == gidPackageHeaderRowID).OrderBy(d => d.ProductID).ToList();
this.SetPackageDetailsList(packageDetails);

i'm facing error in SetPackageDetailsList method as follow :

InvalidOperationException: Error generated for warning 'Microsoft.EntityFrameworkCore.Infrastructure.DetachedLazyLoadingWarning': 
An attempt was made to lazy-load navigation property 'DefaultVisitProductHeaderRow' on detached entity of type 'DefaultVisitProductDetailsProxy'. Lazy-loading is not supported for detached entities or entities that are loaded with 'AsNoTracking()'. This exception can be suppressed or logged by passing event ID 'CoreEventId.DetachedLazyLoadingWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.

Here is SetPackageDetailsList's body :

private void SetPackageDetailsList(List<DefaultVisitProductDetails> lstPackageDetails)
        {
            TempData["_lstPackageDetails"] = JsonConvert.SerializeObject(lstPackageDetails, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects }); // Cause error!
        }

Where is the problem & how to solve it?

Thanks in advance


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>