1
votes

I'm trying to return to application a list to application via Json (using Ajax). I already tried in two ways:

Using a select

var query = (from ad in db.AddressNamePlaces.AsNoTracking()
                     where ad.cepPlace == zipCode.Replace("-", string.Empty)
                     select ad ).ToList();

.Where(Expression) method:

var query = db.AddressNamePlaces.AsNoTracking().Where(l => l.cepPlace == zipCode.Replace("-", string.Empty)).ToList();

Both of them works fine.

Considerations: the related tables in AddressNamePlace has over 770k records, and if I use without .AsNoTracking() method, the application returns to Json an OutOfMemoryException. The Entity Framework instantiate a lot of objects to each record, and the .AsNoTracking() method avoid it.

If I use with the queries above, I'm getting the following error: "When an object is returned with a NoTracking merge option, Load can only be called when the EntityCollection or EntityReference does not contain objects."

So, what's wrong here?

1

1 Answers

1
votes

I find a solution for this case. Before doing my query, I needed to set to false the Attribute "ProxyCreationEnabled", the code:

db.Configuration.ProxyCreationEnabled = false;
        var query = db.AddressNamePlaces.AsNoTracking().Where(l => l.cepPlace == zipCode.Replace("-", string.Empty)).ToList();