Does Entity Framework support Multi-Threading?

Ed Landau

I am writing a C# .NET4.5 Console application targeting the Entity Framework 6.1.3. I am using the Unit Of Work paradigm as follows:

public class UnitOfWork : IUnitOfWork, IDisposable
{
    private readonly DataContext _context;
    private readonly List<object> _repositories = new List<object>();
    public UnitOfWork(DataContext context)
    {
        _context = context;
        _context.Configuration.LazyLoadingEnabled = false;
    }

    public IRepository<T> GetRepository<T>() where T : class
    {
        //try to get existing repository
        var repo = (IRepository<T>)_repositories.SingleOrDefault(r => r is IRepository<T>);
        if (repo == null)
        {
            //if not found, create it and add to list
            _repositories.Add(repo = new EntityRepository<T>(_context));
        }
        return repo;
    }

    public int Commit()
    {
        return _context.SaveChanges();
    }


    public bool AutoDetectChanges
    {
        get { return _context.Configuration.AutoDetectChangesEnabled; }
        set { _context.Configuration.AutoDetectChangesEnabled = value; }
    }

And my Repository like this:

   public class EntityRepository<T> : IRepository<T> where T: class 
    {
        protected readonly DbContext Context;
        protected readonly DbSet<T> DbSet;
    public EntityRepository(DbContext context)
    {
        Context = context;            
        DbSet = Context.Set<T>();
    }

    public IQueryable<T> All()
    {
        return DbSet;
    }
    ….. other functions….

    public virtual void Add(T entity)
    {        
        DbEntityEntry dbEntityEntry = Context.Entry(entity);
        if (dbEntityEntry.State != EntityState.Detached)
        {
            dbEntityEntry.State = EntityState.Added;
        }
        else
        {
            DbSet.Add(entity);
        }
    }
    }

I call these like this:

 var rep = _uow.GetRepository<TableOfPies>();
 rep.Add(Pie);
 _uow.Commit();

My Console application has multiple threads, each of which will at some point want to Update / Edit / Add to the same tables in my cloud-based SQL Server database.

I have implemented thread-safe code for my other code using locks but I am not aware of how I can make Entity thread-safe? Right now, I get the following error:

INNER EXCEPTION: New transaction is not allowed because there are other threads running in the session.

I have looked online and have not been able to find much about Entity and multi-threading. I have heard that Entity does not support multi-threaded applications but find that heard to believe. Any pointers would be greatly appreciated.

Gabriel Luci

The documentation for DataContext states:

Any instance members are not guaranteed to be thread safe.

And that's what I've experienced too. I've tried to do what you're doing and I've seen bizarre errors that backup the idea that it is not thread safe.

You will have to create a new DataContext instance in each thread.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Entity Framework Context per request in ASP.NET and Multi Threading

From Dev

Does C++ provide built in multi threading support?

From Dev

Does Swift have any native concurrency and multi-threading support?

From Dev

Does Entity Framework 6 support .NET 4.0?

From Dev

Does Entity Framework support parallel async queries?

From Dev

does entity framework support User Defined Data Types?

From Dev

Does Entity Framework Code First support this kind of mapping?

From Dev

MVC scaffolding does not support Entity Framework 6 or later

From Dev

Does Entity Framework 6.1 support an XML data type natively?

From Dev

Does Entity Framework support differing data types in the model?

From Dev

Does Entity Framework support Oracle 10g?

From Dev

Entity Framework connect to Oracle: ODP for .NET "does not support time"

From Dev

Entity framework 2.0 does not support DNX version 5.0

From Dev

Modification of Utility Method to Support Multi-Threading

From Dev

How to set threading mode in SQLite with Entity Framework?

From Dev

Multiple database support with entity framework

From Dev

does multi threading improve performance? scenario java

From Dev

Will be whether multi-threading support in mongodb ( without locking database)

From Dev

Does Entity Framework support when deleted a record and set the foreign key to null?

From Dev

Does/Will Entity Framework 6 support the new Sql Azure & Azure Active Directory Authentication

From Dev

Does Entity Framework support when deleted a record and set the foreign key to null?

From Dev

Multi threading

From Dev

Multi threading

From Dev

When will Breeze support Entity Framework 6?

From Dev

Entity Framework and SQL Server optimistic concurency support

From Dev

Which versions of the Entity Framework support compiled queries?

From Java

Multi-async in Entity Framework 6?

From Dev

What threading algorithm(s) does Python's IMAP library support?

From Dev

How does Entity Framework entity loading work?

Related Related

  1. 1

    Entity Framework Context per request in ASP.NET and Multi Threading

  2. 2

    Does C++ provide built in multi threading support?

  3. 3

    Does Swift have any native concurrency and multi-threading support?

  4. 4

    Does Entity Framework 6 support .NET 4.0?

  5. 5

    Does Entity Framework support parallel async queries?

  6. 6

    does entity framework support User Defined Data Types?

  7. 7

    Does Entity Framework Code First support this kind of mapping?

  8. 8

    MVC scaffolding does not support Entity Framework 6 or later

  9. 9

    Does Entity Framework 6.1 support an XML data type natively?

  10. 10

    Does Entity Framework support differing data types in the model?

  11. 11

    Does Entity Framework support Oracle 10g?

  12. 12

    Entity Framework connect to Oracle: ODP for .NET "does not support time"

  13. 13

    Entity framework 2.0 does not support DNX version 5.0

  14. 14

    Modification of Utility Method to Support Multi-Threading

  15. 15

    How to set threading mode in SQLite with Entity Framework?

  16. 16

    Multiple database support with entity framework

  17. 17

    does multi threading improve performance? scenario java

  18. 18

    Will be whether multi-threading support in mongodb ( without locking database)

  19. 19

    Does Entity Framework support when deleted a record and set the foreign key to null?

  20. 20

    Does/Will Entity Framework 6 support the new Sql Azure & Azure Active Directory Authentication

  21. 21

    Does Entity Framework support when deleted a record and set the foreign key to null?

  22. 22

    Multi threading

  23. 23

    Multi threading

  24. 24

    When will Breeze support Entity Framework 6?

  25. 25

    Entity Framework and SQL Server optimistic concurency support

  26. 26

    Which versions of the Entity Framework support compiled queries?

  27. 27

    Multi-async in Entity Framework 6?

  28. 28

    What threading algorithm(s) does Python's IMAP library support?

  29. 29

    How does Entity Framework entity loading work?

HotTag

Archive