Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - Load only Selected properties

I have no hands on experience it EF and hence dont know the relevence of the question.

Suppose I have tables named Student (StudentId, Name, Username, Address, DOB, DeptId, NavigationProp1Id.... ) and in Department table (Deptd, DeptName., NavigationProPid). So if a table structure follows like this, when I use 'contex.Studnets ' I can get all prpoerties along with it including navigation properties and if table 2 has other navigation properties it can also be loaded. Am I correct?

If so whether it cause any performance issue? Can I load only selected properties from Entity like only UserName, Address from Student entity?

like image 552
BonDaviD Avatar asked Nov 10 '11 07:11

BonDaviD


People also ask

How do I load navigation properties in Entity Framework Core?

You can explicitly load a navigation property via the DbContext. Entry(...) API. You can also explicitly load a navigation property by executing a separate query that returns the related entities.

How do I set eager loading in Entity Framework?

Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by use of the Include method. For example, the queries below will load blogs and all the posts related to each blog. Include is an extension method in the System.

What is .include in Entity Framework?

Entity Framework Classic Include The Include method lets you add related entities to the query result. In EF Classic, the Include method no longer returns an IQueryable but instead an IncludeDbQuery that allows you to chain multiple related objects to the query result by using the AlsoInclude and ThenInclude methods.

How do I enable lazy loading in Entity Framework?

Lazy loading with proxiesUseLazyLoadingProxies() . UseSqlServer(myConnectionString)); EF Core will then enable lazy loading for any navigation property that can be overridden--that is, it must be virtual and on a class that can be inherited from. For example, in the following entities, the Post.


2 Answers

No navigation properties are not loaded immediately. They are loaded either explicitly if you use Include method or lazily when you access them for the first time (that is also reason why you see them in debugger = access through debugger caused lazy loading).

You can load only selected properties - it is called projection. The limitation is that you cannot load Student entity with subset of properties. You need either new class or anonymous type:

var query = context.Students.Select(x => new 
                {
                   x.UserName,
                   x.Address 
                }); 
like image 124
Ladislav Mrnka Avatar answered Oct 16 '22 15:10

Ladislav Mrnka


When using Entity Framework (EF) your best solution is to query the EF context using LINQ (http://msdn.microsoft.com/en-us/library/bb308959.aspx). LINQ can be used to query across relationships, so in your scenario your code would look like the following:

var joinedlist = context.Student.Join(
                // The table we wish to join to the Student table
                context.Department,
                // Item on student table we want to join from
                studentItem => studentItem.DeptId,
                // Item on department table we want to join to
                departmentItem => departmentItem.Deptd, 
                // New object just holding the values we wish to retrieve from the joined tables
                (studentItem, departmentItem) => new {
                        StudentId = studentItem.StudentId, 
                        StudentUsername = studentItem.Username,
                        StudentAddress = studentItem.Address,
                        DepartmentName = departmentItem.DeptName, 
                        Navigation = StudentItem.NavigationProp1Id
                    }
            );

The above code will generate for you a queryable list, but you can do many more things with LINQ. For example selecting a subset of data and filtering your results:

var singleItem = joinedlist
                // Filter our joined list
                .Where(item => item.StudentId.Equals(1))
                // Select only a subset of columns
                .Select(item => new {item.StudentUsername, item.StudentAddress})
                // Return only a single item
                .FirstOrDefault();

Regarding performance - I would suggest getting hold of a profiler which can show you the SQL output of your LINQ statements on EF. This really helps when it comes to understanding lazy loading and where a misplaced .ToList() may be returning your entire DB!

like image 33
steelcm Avatar answered Oct 16 '22 16:10

steelcm