Using include doesn't change the behavior

c# entity-framework entity-framework-6 performance sql-server

Question

Could someone please explain the distinction between:

 var query = awlt.People.Include(p => p.EmailAddresses)
                    .Where(p => p.LastName.Equals(lastName))
                    .SelectMany(a => a.EmailAddresses)
                    .Select(a => a.EmailAddress1); 

 var query = awlt.People
                    .Where(p => p.LastName.Equals(lastName))
                    .SelectMany(a => a.EmailAddresses)
                    .Select(a => a.EmailAddress1);

Without being aware of the distinction, I obtain the same outcomes in both situations. Is using the Eager Loading necessary?Include ?

1
6
4/30/2016 11:19:56 PM

Accepted Answer

Just the first query is retrieving the related data using Aware of loading (and yes, anxious loading is accomplished by utilizing theInclude way as you probably anticipated) and the second query by utilizing the default sluggish loading. But as your query only yields results,EmailAddresses owing to theSelect() and SelectMany() the Include() the behavior is unaffected by the approach operations watch whenInclude() In your example, the approach is important. I'll give one example to prove that in the lines that follow:

When you need the related data for every row of the primary table that has been obtained, anxious loading is often more effective than the other two types of loading related entities. Additionally, quickly loading will be excellent practice to reduce additional server queries when relations arenot too much. But sluggish loading may be a wise alternative if you anticipate not needing a property right away. Additionally, eager loading is a smart move if your database context is destroyed and slow loading is no longer an option. Consider the following code to demonstrate eager loading versus lazy loading:

public List<Person> GetEmailAddresses()
{
    using (yourEntities awlt = new yourEntities())
    {
        var query = awlt.People
                .Where(p => p.LastName.Equals(lastName));
        return query.ToList();
    }
}

You cannot load the linked entity in a lazy manner after executing this method since the database has been deleted. Try this to demonstrate:

var query = GetEmailAddresses();
foreach (var item in query.SelectMany(a => a.EmailAddresses).Select(a => a.EmailAddress1))
{
    MessageBox.Show(item);                
}

The following error will appear:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

However, if you alter theGetEmailAddresses to employ Eager Loading as follows:

public List<Person> GetEmailAddresses()
{
    using (yourEntities awlt = new yourEntities())
    {
        var query = awlt.People.Include("EmailAddresses")
                .Where(p => p.LastName.Equals(lastName));
        return query.ToList();
    }
}

The code following should then function properly:

var query = GetEmailAddresses();
foreach (var item in query.SelectMany(a => a.EmailAddresses).Select(a => a.EmailAddress1))
{
    MessageBox.Show(item);                
}

Therefore, the Aware of loading would be a better option if your database context were to be disposed of.

6
5/7/2016 5:28:55 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow