Search code examples
c#entity-frameworkentity-framework-6.net-4.8

Why EF6 allocate memory for some DbSets?


I have a simple dbcontext, It has three set in it. When I debug it using Windbg to see what object is live i see some dbset.

So, My question is why some dbset is in heap?

Here is my code:

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

Main method:

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

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

Windbg Command: !dumpheap -type live enter image description here


Solution

  • There is some sort of weird stuff with naming entity.

    If your entity name contains "delive", It will allocate memory.

    I don't know if it is a bug or what, change your entity name (Delivery) and allocation will go away.