0

I've got a question regarding self references in the entity framework, just to determine the load of the app.

Unfortunately I have some relationships in my tables. Due of this, I have some self referencing objects. For example: A company has some customer, a customer belongs to some entries, these entries are mapped to an company which contains the different customers...

(Company -> Customer -> Entry -> Company -> Customer ....)

When I want to serialize this in a json string, I will get the error message regarding self referencing loops (it's clear) By making this query with the entity framework, I don't get an error message. But when I take a look in the debugger, I can see the "loop".

My questions: Does it cause a high performance load on my server? Or is this different handled, so I can ignore this self references in c#?

Otherwise I think I have to make changes regarding the relationships in the tables...

I just want to prevent problems about the server load when I've more entries in the tables.

Thanks in advance.

AJRames
  • 125
  • 1
  • 11

1 Answers1

0

I think that article about Lazy Loading would be helpful for you. Take are look at next parts

Lazy loading is the process whereby an entity or collection of entities is automatically loaded from the database the first time that a property referring to the entity/entities is accessed. 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.

...

Lazy loading and serialization don’t mix well, and if you aren’t careful you can end up querying for your entire database just because lazy loading is enabled. Most serializers work by accessing each property on an instance of a type. Property access triggers lazy loading, so more entities get serialized. On those entities properties are accessed, and even more entities are loaded.

Answer for you perfromance question - Entity Framework will load data for your referenced properties only when you access it.

And if you want to serialize Entity Framework classes, you can disable LazyLoading and than eager load only data, which you want to serialize

public Company GetCompany(int id)
{
    using (var context = new DBData())
    {
        context.Configuration.LazyLoadingEnabled = false;
        return context.Set<Company>()
            .Include(x => x.Customer)//you can add other includes which you want
            .Where(x => x.Id == id).FirstOrDefault();
    }
}

But I highly recommend to create other class with only those properties which you want serialize, map data from EF class to that new class (using AutoMapper or manually) and than serialize it with safety.

YuriyP
  • 3,852
  • 4
  • 24
  • 34
  • I'd also like to note that if one did want to keep lazing loading active, references can be handled in different ways during serialization (examples from another question using Json.NET: http://stackoverflow.com/a/8711702/1234773) – Josh Coulter Oct 07 '16 at 10:40
  • I think a main problem is my understanding how the entity framework works. I thought I will have an endless loop in my c# code. But now it seems to be the way how it works. It occurs even when I have only two entities connected with primaray and foreign key. I can open the other entity by opening the proberties of the first one, and I can open the first object properties from this current object and so on. So I thought it will be an endless loop, but it doesn't seem to be so... – AJRames Oct 10 '16 at 06:10
  • 1
    EF do not load navigation properties by default. It will load it when you access it first time (Lazy Loading) or when you specify it in `.Include` (Eager Loading). So when you wrote something like this `var item = Context.Set().Find(1); var customer = item.Customer;` EF will do two queries to database. First one load only company fields (without navigation properties) when you call `.Find()`, and second query would be performed when you access `.Customer` property and would load customer fields (also without navigation properties). – YuriyP Oct 10 '16 at 10:15