using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
public static void Main()
using var ctx = new MyDbContext();
var customer = new Customer(Industry.IndustryMedia);
ctx.Customers.Attach(customer);
Console.WriteLine("Main thread count of Industry.IndustryMedia.Customers: {0}", Industry.IndustryMedia.Customers.Count);
ThreadPool.QueueUserWorkItem(state =>
Console.WriteLine("Thread {0} count of Industry.IndustryMedia.Customers: {1}", Thread.CurrentThread.ManagedThreadId, Industry.IndustryMedia.Customers.Count);
Console.WriteLine("Only now calling saving changes");
public class MyDbContext : DbContext
public DbSet<Customer> Customers => Set<Customer>();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseInMemoryDatabase("MyDb");
public long Id { get; private set; }
public Industry Industry { get; private set; }
public Customer(Industry industry)
public long Id { get; private set; }
public string Name { get; }
public static readonly Industry IndustryMedia = new("Media");
private Industry(string name)
public virtual ICollection<Customer> Customers { get; private set; } = new List<Customer>();