为什么 EF6 会为一些 DbSet 分配内存?

问题描述 投票:0回答:1

我有一个简单的 dbcontext,里面有三个集合。 当我使用 Windbg 调试它以查看活动对象时,我看到了一些 dbset。

所以,我的问题是为什么有些 dbset 在堆中?

这是我的代码:

public class Delivery
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

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

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

internal class TestContext : DbContext
{
    public TestContext() : base("db") { }

    public DbSet<Delivery> DeliveryMethods { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }
}

主要方法:

    static void Main(string[] args)
    {
        using (var db = new TestContext())
        {

        }
        Console.WriteLine("done");
        Console.ReadKey();
    }

Windbg 命令:!dumpheap -type live

c# entity-framework entity-framework-6 .net-4.8
1个回答
0
投票

DbContext 基类初始化(通过反射)任何 DbSet 属性。 请参阅文档中的DbContext with DbSet properties

此外,DbContext 会自动为这些属性中的每一个调用 setter 以设置适当的 DbSet 的实例。

您永远不需要自己实际创建 DbSet。

© www.soinside.com 2019 - 2024. All rights reserved.