Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent cyclic loading of related entities in Entity Framework Code First

I'm new to Entity Framework and am trying to learn how to use Code First to load entities from the database.

My model contains a user:

public class User
{
    public int UserID { get; set; }

    [Required]
    public string Name { get; set; }

    // Navigation Properties
    public virtual ICollection<AuditEntry> AuditEntries { get; set; }
}

Each user can have a set of audit entries each of which contains a simple message:

public class AuditEntry
{
    public int AuditEntryID { get; set; }

    [Required]
    public string Message { get; set; }

    // Navigation Properties
    public int UserID { get; set; }
    public virtual User User { get; set; }
}

I have a DBContext which just exposes the two tables:

public DbSet<User> Users { get; set; }
public DbSet<AuditEntry> AuditEntries { get; set; }

What I want to do is load a list of AuditEntry objects containing the message and the related User object containing the UserID and Name properties.

List<AuditEntry> auditEntries = db.AuditEntries.ToList();

Because I have my navigation properties marked as virtual and I haven't disabled lazy loading, I get an infinitely deep object graph (each AuditEntry has a User object, which contains a list of the AuditEntries, each of which contains a User object, which contains a list of AuditEntries etc)

This is no good if I then want to serialize the object (for example to send as the result in a Web API).

I've tried turning off lazy loading (either by removing the virtual keywords from my navigation properties in the model, or by adding this.Configuration.LazyLoadingEnabled = false; to my DBContext). As expected this results in a flat list of AuditEntry objects with User set to null.

With lazy loading off, I've tried to eager load the User like so:

var auditentries = db.AuditEntries.Include(a => a.User);

but this results in the same deep / cyclic result as before.

How can I load one level deep (e.g. include the user's ID and name) without also loading back-references / following navigation properties back to the original object and creating a cycle?

like image 729
Matt Wilson Avatar asked Nov 19 '12 11:11

Matt Wilson


People also ask

How do I remove circular references in Entity Framework?

Notice the navigation property check boxes, you can deselect them if you don't want them to be generated. To solve your circular reference problem, make sure only one or none are checked, not both.

How do I stop lazy loading in Entity Framework?

We can disable lazy loading for a particular entity or a context. To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false.

Which is the process to delay the loading of related objects until it is required?

Lazy loading is the practice of delaying load or initialization of resources or objects until they're actually needed to improve performance and save system resources.

How do I enable lazy loading code first in Entity Framework?

Lazy loading means delaying the loading of related data, until you specifically request for it. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.


1 Answers

After much hacking, I've come up with the following potential solution using a dynamic return type and projection in my Linq query:

public dynamic GetAuditEntries()
{
    var result = from a in db.AuditEntries
                 select new
                 {
                     a.AuditEntryID,
                     a.Message,
                     User = new
                     {
                         a.User.UserID,
                         a.User.Username
                     }
                 };

    return result;
}

This produces (internally) the following SQL which seems sensible:

SELECT 
[Extent1].[AuditEntryID] AS [AuditEntryID], 
[Extent1].[Message] AS [Message], 
[Extent1].[UserID] AS [UserID], 
[Extent2].[Username] AS [Username]
FROM  [dbo].[AuditEntries] AS [Extent1]
INNER JOIN [dbo].[Users] AS [Extent2] ON [Extent1].[UserID] = [Extent2].[UserID]

This produces the results that I'm after, but it seems a bit long winded (especially for real life models that would be significantly more complex than my example), and I question the impact this will have on performance.

Advantages

  • This gives me a lot of flexibility over the exact contents of my returned object. Since I generally do most of my UI interaction / templating on the client side, I frequently find myself having to create multiple versions of my model objects. I generally need a certain granularity over which users can see which properties (e.g. I might not want to send every user's email address to low-privilege user's browser in an AJAX request)

  • It allows entity framework to intelligently build the query and only select the fields that I have chosen to project. For example, inside each top level AuditEntry object, I want to see User.UserID and User.Username but not User.AuditEntries.

Disadvantages

  • The returned type from my Web API is no longer strongly typed so I couldn't create a strongly typed MVC view based on this API. As it happens this is not a problem for my particular case.

  • Projecting manually in this way from a large / complex model could result in a lot of code, seems like a lot of work and has the potential to introduce errors in the API. This would have to be carefully tested.

  • The API method becomes tightly coupled with the structure of the model and since this is no longer fully automated based on my POCO classes, any changes made to the model would have to be reflected in the code that loads them.

Include method?

I'm still a little confused about the use of the .Include() method. I understand that this method will specify that related entities should be "eager loaded" along with the specified entity. However, since the guidance seems to be that navigation properties should be placed on both sides of a relationship and marked as virtual, the Include method seems to result in a cycle being created which has a significant negative impact on it's usefulness (especially when serializing).

In my case the "tree" would look a little like:

AuditEntry
    User
        AuditEntries * n
            User * n
                etc

I'd be very interested to hear any comments about this approach, the impact of using dynamic in this way or any other insights.

like image 146
Matt Wilson Avatar answered Sep 27 '22 18:09

Matt Wilson