Entity Framework - redundant connection string

后端 未结 4 1718
眼角桃花
眼角桃花 2021-02-08 18:57

I\'m using Entity Framework 4 in my project. The Framework has created its own connection string, so my web.config connectionStrings section file looks following:

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-08 19:54

    I'll provide complete implementation I did to resolve this issue (based on gandjustas hints). I've written a simple wrapper for the context, which can be used in the following manner:

    using (var wrapper = new ContextWrapper())
    {
        // do your stuff based on wrapper.Context
    }
    

    Type ContextWrapper is a template which simply wraps the context which is just constructed in a slightly other way (using only one connection string) and then exposed by property. Its internal implementation is placed below:

    public class ContextWrapper : IDisposable
        where TContext : ObjectContext
    {
        private TContext _context;
        private EntityConnectionManager _manager;
        private bool _disposed;
    
        public ContextWrapper()
            : this(true)
        {
        }
    
        public ContextWrapper(bool lazyLoadingEnabled)
        {
            _disposed = false;
            _manager = new EntityConnectionManager();
            _context = (TContext)Activator.CreateInstance(typeof(TContext), _manager.Connection);  
            _context.ContextOptions.LazyLoadingEnabled = lazyLoadingEnabled;
        }
    
        ~ContextWrapper()
        {
            Dispose(false);
        }
    
        public TContext Context
        {
            get { return _context; }
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
    
        }
    
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (_manager != null)
                    {
                        _manager.Dispose();
                        _manager = null;
                    }
                    var ctx = _context as IDisposable;
                    if (ctx != null)
                    {
                        ctx.Dispose();
                        _context = null;
                    }
                }
            }
            _disposed = true;
        }
    }
    

    You can see the usage of custom class named EntityConnectionManager:

    internal class EntityConnectionManager : IDisposable
    {
        private DbConnection _connection;
        private EntityConnection _entityConnection;
    
        private volatile bool _disposed;
    
        public EntityConnectionManager()
        {
            var workspace = new MetadataWorkspace(Setting.MetadataWorkspacePaths.Split('|'), new[] { Assembly.ReflectionOnlyLoad(Setting.MetadataAssemblyNameToConsider) });
    
            _connection = new SqlConnection(Setting.ConnectionString);
            _entityConnection = new EntityConnection(workspace, _connection);
            _disposed = false;
        }
    
        public EntityConnection Connection
        {
            get { return _entityConnection; }
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (_connection != null)
                    {
                        _connection.Dispose();
                        _connection = null;
                    }
                    if (_entityConnection != null)
                    {
                        _entityConnection.Dispose();
                        _entityConnection = null;
                    }
                }
            }
            _disposed = true;
        }
    }
    

    So now you can have one connection string:

    
          
    
    

    and metadata defined in app settings section (the second key points to assembly where your domain model is actually stored):

    
      
      
    
    

    The logic for type Setting is straightforward, as it just pulls out the settings from configuration file.

提交回复
热议问题