EF Core Creating Object with “infinite” depth

无人久伴 提交于 2020-01-06 05:36:07

问题


I have an M to N relationship on my Database. I took the sample Database "sakila" from the MySql page. I have set up my Model Objects as follows.

Film Table

public partial class Film
    {
        public Film()
        {
            FilmActor = new HashSet<FilmActor>();
            FilmCategory = new HashSet<FilmCategory>();
            Inventory = new HashSet<Inventory>();
        }

        //Some Properties


        public ICollection<Inventory> Inventory { get; set; }
    }

Linking Table Inventory

public partial class Inventory
    {
        public Inventory()
        {
            Rental = new HashSet<Rental>();
        }

        public int InventoryId { get; set; }
        public short FilmId { get; set; }
        public byte StoreId { get; set; }
        public DateTimeOffset LastUpdate { get; set; }

        public Film Film { get; set; }
        public Store Store { get; set; }
        public ICollection<Rental> Rental { get; set; }
    }

Store Table

public partial class Store
    {
        public Store()
        {
            Customer = new HashSet<Customer>();
            Inventory = new HashSet<Inventory>();
            Staff = new HashSet<Staff>();
        }

        //More Properties
        public ICollection<Inventory> Inventory { get; set; }

    }

When I go to retrieve the Data through a Repository I get back a list of Store objects that has a list of Inventory that has a list of films that has a list of stores... In other words: store[2]->inventory[2270]->film->store[2]->inventory... ad infinitum.

So how do I make this stop when my Model gets to the film objects?


回答1:


Entity Framework tracks your database objects by default when you query them. If you fetch child or parents objects in your query, either through lazy or eager loading, they will be "stored" on the (local) context.

This means that whenever you enter debug mode and hit a breakpoint, you can infinitely step through your object tree. At runtime, this has no real effect on performance.



来源:https://stackoverflow.com/questions/53174003/ef-core-creating-object-with-infinite-depth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!