Entity Framework 4 selective lazy loading properties

后端 未结 4 530
终归单人心
终归单人心 2020-11-30 07:47

Is it possible to load an entity excluding some properties? One of this entity\'s properties is expensive to select. I would like to lazy load this property. Is that possibl

相关标签:
4条回答
  • 2020-11-30 08:01

    Now that you have read everyone's reply, I will give you the correct answer. EF does not support lazy loading of properties. However it does support a much powerful concept then this. It's called table splitting where you can map a table to two entities. Say a product table in the the database can be mapped to product entity and ProductDetail entity. You can then move the expensive fields to the ProductDetail entity and then create a 1..1 association between prodcut and productdetail entity. You can then lazy load the productdetail association only when you need it. In my performance chapter of my book, I have a recipe called. 13-9. Moving an Expensive Property to Another Entity

    Hope that helps!

    Julie Lerman has an article on how to split a table

    0 讨论(0)
  • 2020-11-30 08:04

    stimms is correct, but be careful while using lazy loading. You may have performance issues and not realize the property is getting loaded at a specific location in your code. This is because it loads the data when you use the property

    I prefer to use explicit loading. This way you know when they get loaded and where. Here's a link that gives an example for the LoadProperty http://sankarsan.wordpress.com/2010/05/09/ado-net-entity-framework-data-loading-part-2/

    You can also you Eager Loading by using the Include method. Example here:http://wildermuth.com/2008/12/28/Caution_when_Eager_Loading_in_the_Entity_Framework

    0 讨论(0)
  • 2020-11-30 08:09

    With a scalar property, the only way to selectively not load a certain property is to project in ESQL or L2E:

    var q = from p in Context.People
            select new
            {
                Id = p.Id,
                Name = p.Name // note no Biography
            };
    

    +1 to Dan; doing this lazily is worse than loading it up-front. If you want to control loading, be explicit.

    0 讨论(0)
  • 2020-11-30 08:12

    Given a query over an EntityFramework DbSet, where the targeted entity contains a BigProperty and a SmallProperty, When you're trying to only access the SmallProperty without loading the BigProperty in memory :

    //this query loads the entire entity returned by FirstOrDefault() in memory
    //the execution is deferred during Where; the execution happens at FirstOrDefault
    db.BigEntities.Where(filter).FirstOrDefault()?.SmallProperty;
    
    //this query only loads the SmallProperty in memory
    //the execution is still deferred during Select; the execution happens at FirstOrDefault
    //a subset of properties can be selected from the entity, and only those will be loaded in memory
    db.BigEntities.Where(filter).Select(e=>e.SmallProperty).FirstOrDefault();
    

    Therefore you could exploit this behaviour to only query the BigProperty where you actually need it, and use select statements to explicitly filter it out everywhere else.

    I tested this with the Memory Usage functionality from the Visual Studio debug Diagnostic Tools.

    0 讨论(0)
提交回复
热议问题