Entity Framework Thread Safety

前端 未结 3 1905
夕颜
夕颜 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:15

    More that one thread operating on a single Entity Framework context is not thread safe.

    A separate instance of context for each thread is thread-safe. As long as each thread of execution has its own instance of EF context you will be fine.

    In your example, you may call that code from any number of threads concurrently and each will be happily working with its own context.

    However, I would suggest implementing a 'using' block for this as follows:

    // this method is called from several threads concurrently
    public void IncrementProperty()
    {
       using (var context = new MyEntities())
       {
          context.SomeObject.SomeIntProperty++;
          context.SaveChanges();
       }
    }
    
    0 讨论(0)
  • 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<YourContext>(); also you need to change your repository by receiving a Func<YourContext>

    0 讨论(0)
  • 2020-11-27 18:37

    I believe "SomeObject.SomeIntProperty" is static. This has nothing to do with Entity being threadsafe. If you are writing to a Static variables in a multithreaded environment you should always wrap them with a double check lock to ensure thread safety.

    0 讨论(0)
提交回复
热议问题