EF Core 使用“无限"创建对象深度 [英] EF Core Creating Object with "infinite" depth

查看:20
本文介绍了EF Core 使用“无限"创建对象深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有 M 到 N 的关系.我从 MySql 页面获取了示例数据库sakila".我已经按如下方式设置了我的模型对象.

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.

电影表

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; }
    }

链接表库存

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; }
    }

存储表

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; }

    }

当我通过存储库检索数据时,我会返回一个 Store 对象列表,其中包含一个 Inventory 列表,其中包含一个包含商店列表的电影列表...换句话说: store[2]->inventory[2270]->film->store[2]->inventory...无穷无尽.

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?

推荐答案

Entity Framework 在您查询数据库对象时默认跟踪它们.如果您在查询中获取子对象或父对象,无论是通过延迟加载还是预先加载,它们都将存储"在(本地)上下文中.

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.

这篇关于EF Core 使用“无限"创建对象深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆