3

How disable all lazy loading

After generation model from database first I have

public partial class company
{
    public int id { get; set; }
    public string name { get; set; }
    public virtual ICollection<user> user { get; set; }
}

public partial class user
{
    public int id { get; set; }
    public int company_id { get; set; }
    public virtual company company { get; set; }
}

I want load only user and their company

db = new Entities();
db.Configuration.ProxyCreationEnabled = false;
db.Configuration.LazyLoadingEnabled = false;
var result = db.user.Include(x => x.company).ToList();

I don`t want load result[0].company.user

Now collection filled with all users of company

result[0].company.user must be null

if i want load result[0].company.user I want use .Include(x => x.company.user)

0

1 Answer 1

4

This is not lazy-loading but a different concept called relationship fix-up. In short it just keeps navigation properties in sync with each other. In your case you have user.company navigation property and company.user collection navigation property. You load both user and company and they are attached to the context. Now at certain points (list of such points you can find here) EF performs relationship fix-up. In this case it happens after query is performed against DbSet. EF ensures that if user.company is set - company.user collection should contain that user also, because those are related navigation properties. This user is already attached to the context (you originally loaded it with your query) so no additional queries are made to database, so it's not lazy loading.

With lazy loading, if you have 100 users for company A then companyA.user will contain 100 entries (loaded from database when you access this property). In your case, even if company A has 100 users - companyA.user will contain just 1 user - that one which you originally loaded.

This behaviour is usually fine, though in some cases it might cause troubles - most often this happens when you want to serialize your EF object and step into circular references because of that.

There is no way to disable this behavior that I'm aware of.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes main trouble in serialization in JSON. When I use CodeFirst i don`t use virtual and EF not load any (not included by me) properties. May be I can in DataBase First disable virtual for some properties?
Problem is virtual properties are not related to this behavior. You disabled proxy creation too so no proxies are created anyway. As for serialization - most serializers can work with circular references and not fail with stack overflow.
user.company does not matter - EF uses foreign keys for relationship fix-up. Even if you load user and company via two different queries - relationships between them will be established.
@PavelMayorov that's true, just in this specific example we have just those two properties - user.company and company.user.
@PavelMayorov but I don't say anywhere that proxy creation is related. Quite the contrary - I say in comment above that it's not related.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.