Entity Framework Thread Safety

前端 未结 3 1919
夕颜
夕颜 2020-11-27 17:38

The context objects generated by Entity Framework are not thread-safe.

What if I use two separate entity contexts, one for each thread (and call SaveChanges()<

3条回答
  •  再見小時候
    2020-11-27 18:28

    You can use the factory approach inject your DbContext as a factory instead of an instance perse, take a look at this: https://github.com/vany0114/EF.DbContextFactory

    It's safer and you avoid to hardcode the instance creation into your repositories.

    http://elvanydev.com/EF-DbContextFactory/

    There is an extension to Ninject to do that in a very easy way, just calling the method kernel.AddDbContextFactory(); also you need to change your repository by receiving a Func

提交回复
热议问题