1.线程本地存储区的专用集合对象,并提供对每个逻辑执行线程都唯一的数据槽。
2.数据槽不在其他逻辑线程上的调用上下文之间共享。
class Program
{
static Jason_TestEntities Current
{
get
{
Jason_TestEntities dbContext = CallContext.LogicalGetData("CurrentContext") as Jason_TestEntities;
if (dbContext == null)
{
Console.WriteLine("重新获取DBContext" + Thread.CurrentThread.ManagedThreadId);
dbContext = new Jason_TestEntities();
dbContext.Configuration.ValidateOnSaveEnabled = false;
CallContext.LogicalSetData("CurrentContext", dbContext);
}
return dbContext;
}
} static void Main(string[] args)
{
for (int i = ; i < ; i++)
{
ThreadStart threadStart = new ThreadStart(select);
Thread thread = new Thread(threadStart);
thread.Start();
}
Console.WriteLine("主线程结束");
Console.Read();
} static void select()
{
Console.WriteLine("线程开始" + Thread.CurrentThread.ManagedThreadId);
Current.wolf_example.Add(new wolf_example()
{
Name = "name" + Current.wolf_example.Count(),
Money = Current.wolf_example.Count(),
});
Current.SaveChanges();
Console.WriteLine("线程结束" + Thread.CurrentThread.ManagedThreadId);
}
}