0

I'm using Entity Framework Core.

My model classes are:

public class Material
{
    public long Id { get; set; }
    public string Name { get; set; }
    public double Weight { get; set; }

    public long? ParentId { get; set; }
    public Material Parent { get; set; }

    public State State { get; set; }
}

public class State
{
    public long Id { get; set; }
    public string Name { get; set; }
}

My problem is that EF will load Parent entity even I don't include it in my query.

public IEnumerable<Material> GetAllMaterials()
{
    return _context.Materials
            .Include(m => m.State)
            .ToList();
    }

Related configuration is this.

builder.Entity<Material>()
            .HasOne(m => m.Parent)
            .WithMany()
            .HasForeignKey(m => m.ParentId);
builder.Entity<Material>()
            .HasOne(m => m.State)
            .WithMany();

Is there away that I can prevent this happens? I need only parentId, not the whole entity.

I have tried the [NotMapped] attribute and virtual for Parent property.

Here is example json what my controller is returning http://paste.dy.fi/5xa?clr=json&style=default&row=1&tabsize=0

EDIT: More researching: If I make this query:

_context.Materials
            .Include(m=>m.Location)
            .Include(m => m.State)
            .Include(m => m.Status)
            .Include(m => m.RemovalMethod)
            .Where( m =>m.Parent!= null)
            .ToList();

I will not get parents. And they are not coming with child entities. JSON result is this http://paste.dy.fi/Ozk

Here is one more example how this is working:

_context.Materials
            .Include(m=>m.Location)
            .Include(m => m.State)
            .Include(m => m.Status)
            .Include(m => m.RemovalMethod)
            .Include(m => m.Parent)
            .Where( m =>m.Parent!= null)
            .ToList();

JSON result is this http://paste.dy.fi/Oie This last one make sence because I include Parent.

Janne Harju
  • 976
  • 1
  • 14
  • 29
  • It's not _loading_ it, it's _attaching_ it because it already saw the parent entity. – CodeCaster Aug 12 '17 at 07:09
  • 1
    It's not possible. The reason is explained [here](https://stackoverflow.com/questions/43374526/ef-core-include-in-many-to-many-relation/43374870#43374870) Normally it shouldn't be a problem, I have no idea why it is for you. But you can always create another class and use `Select` query. – Ivan Stoev Aug 12 '17 at 07:33
  • Do you mean to say EF is "Eager Loading" the `Parent` entity? Or just that you are able to view the parent entity values in the quick-watch window / while debugging? Just try disabling lazy loading in the EF or inspect the object after the context is disposed. – Developer Aug 12 '17 at 08:53
  • Like CodeCaster say the correct word is attatch. So EF is attaching parent already to context and that's why it is coming with child entity. I will try fetch only child entities and watch will that remove parent entities from child. I'm using .Net Core's json serializer by using this attribute [Produces("application/json")] in controller. So I'am watching what controller is returning to browser. There is no problem it is just dumb to get parent entites many times when only one is enough. – Janne Harju Aug 12 '17 at 10:45
  • Developer: I'm using Core so I can't use Lazy loading. And there is example how I include one another entity which will not load if I don't Include it. – Janne Harju Aug 12 '17 at 10:49
  • Thank you Ivan Stoev. Your comment is correct. I didn't find that answer by myself because I use different words in search. So maybe this can help someone else to find answer from other question. – Janne Harju Aug 12 '17 at 11:11

0 Answers0