Entity framework context does not update upon modification

I have a project WPFthat is an architecture n-tierand I use a per / challenge context because it has Direct-databaseandweb-service

Sorry a little long question

Presentation => Business Layer => Data Layer

As datalayerI realized the pattern UnitOfWork.

Initially, when mine DefaultAccessPointwas a property Static, I had problems with multiple threadusing the same context.

I solved this by changing the property DefaultAccessPointto non-static, and it seems that the problem with multiple threads has been resolved.

after fix => (for example) the User can paste the data on the "Application on the first" tab (which takes about 3 minutes) and at the same time access the second tab (to get some data), while both are executed in separate threads.

But after that, Fix Contextdoes not update during modification

The original fix was done in the class DataProviderBase: I changed StaticDataAccessPoint to non-staticand commented on the lines that you see below.

How can I update the context using multithreading?

Any help is appreciated

Code block

This is my class DataProviderBase, each data provider inherits this database.

public abstract class DataProviderBase
{
    public DataProviderBase()
    {
        DefaultAccessPoint = new DataAccessAccessPoint(ConnectionString);
    }

    protected readonly ILogger logger = LoggerFactory.GetLogger();
    private static string _connectionString;
    public static string ConnectionString
    {
        get
        {
            return _connectionString;
        }
        set
        {
            _connectionString = value;
            //COMMENTED as Fix for multiThreading
            //_defaultAccessPoint = new DataAccessAccessPoint(ConnectionString);
        }
    }

    private IDataAccessAccessPoint _defaultAccessPoint;
    public IDataAccessAccessPoint DefaultAccessPoint
    {
        get
        {
             //COMMENTED as Fix for multi Threading
            // Removed statis Default AccessPoint that was causing the issue
            return _defaultAccessPoint; //?? (_defaultAccessPoint = new DataAccessAccessPoint(ConnectionString));
        }
        set { _defaultAccessPoint = value; }
    }


}

This is my DataAccessPoint

public class DataAccessAccessPoint : IDataAccessAccessPoint
{
    private string _connectionString;

    public string ConnectionString
    {
        get
        {
            return _connectionString;
        }
        set
        {
            _connectionString = value;
        }
    }

    private IDataContext context;

    public DataAccessAccessPoint(string connectionString)
    {
        _connectionString = connectionString;
        context = new MyDataContext(_connectionString);
    }

    public virtual bool Save()
    {
        return context.SaveChanges() > 0;
    }

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    //Here i supply the context to my Data access layers
    private IMyDataLayerDA _myDA;
    public IMyDataLayerdDA MydDA
    {
        get { return _myDA ?? (_myDA = new MydDA(context)); }
        set { _myDA = value; }
    }

}

My DataProvider

 public class PersonRoleDataProvider : DataProviderBase, IPersonRoleDataProvider
{
    public MYDTOCLASS AuthenticateUser(string userId)
    {
        return DefaultAccessPoint.MydDA.AuthenticateUser(userId);
    }

    public IEnumerable<MYDTOCLASS> GetRoles(int personId)
    {
        return DefaultAccessPoint.MydDA.GetRoles(personId);
    } 
}

DataProviderAccessPoint

  public class DataProviderAccessPoint
  {

    private static PersonRoleDataProvider _personRoleDataProvider;
    public static PersonRoleDataProvider  PersonRoleDataProvider 
    {
        get
        {
             if(_personRoleDataProvider==null)
             _personRoleDataProvider = new PersonRoleDataProvider();

            return _personRoleDataProvider;
        }
    }

 }
+4

Source: https://habr.com/ru/post/1614252/


All Articles