1.问题的引入

对于某一个数据库的EF操作对象,当执行某一次请求的时候,可能会多次操作数据库,也就是可能创建很多MyDbContext(继承自DbContext对象,EF上下文对象)

EF的使用(DbContext对象的共用问题)

 

 

2.代码创建

当需要使用MyDbContext对象的时候

MyDbContext db=null;

if(HttpContext.Current.Items["MyDbContext"]==null)

{

    db=new MyDbContext();

}

else

{

  db=HttpContext.Current.Itms["MyDbContext"] as MyDbContext;

}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-19
  • 2021-09-25
  • 2022-01-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-22
  • 2021-10-29
  • 2022-12-23
  • 2022-02-14
  • 2021-12-20
  • 2021-10-24
  • 2021-12-05
相关资源
相似解决方案